【经典回放】多种语言系列数据结构算法:快速排序

快速排序(Quicksort)是对冒泡排序的一种改进。

快速排序由C. A. R. Hoare在1960年提出。它的基本思想是:通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此达到整个数据变成有序序列。

直接上干货!

1. C语言:

#include <stdio.h>
#include <stdlib.h>

void QSort(int  A[],int Low,int High)
{
	int i,j,P;
	if(Low>=High) return;
	i=Low;j=High;
	P=A[Low];
	while(i<j)
		{
		while(i<j && P<=A[j]) j--;
		if(i<j)	{A[i]=A[j];i++;	}
		while(i<j && A[i]<P) i++;
		if(i<j)	{A[j]=A[i];j--;	}
		}
	A[i]=P;
	QSort(A,Low,i-1);
	QSort(A,i+1,High);
}

main()
{
	int a[]={49,38,65,97,76,13,27,49},i; 
	QSort(a,0,7);
	for(i=0;i<8;i++)
		printf("%d  ",a[i]);
	printf("\n");

}

 2. C#语言:

快速排序界面
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 快速排序
{
    class elemtype
    {
        public  int low, high;
        public elemtype()
        {
            low = 0; high = 0;
        }
        public elemtype(int LOW, int HIGH)
        {
            low = LOW; high = HIGH;
        }
    }
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 快速排序
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
       public int QSwap(int []A,int pLow,int pHigh)
       {
	        int i,j,P;
	        if (pLow>=pHigh) return -1;
	        i=pLow;j=pHigh;P=A[i];
	        while(i<j)
	    	{
		        while(i<j && P<=A[j])  j--;	//右边的小于支点前移动
		        if(i<j)	{A[i]=A[j];i++;	}
		        while(i<j && A[i]<P)   i++;	//左边的大于支点后移动
		        if(i<j) {A[j]=A[i];j--;	}
		    }
	        A[i]=P;							//最后确定的位置放支点数据
	        return i;
        }       
        public  void SQSort(int []A,int n)
        {
	        //Stack<int>s=new Stack<int>();
            Stack<elemtype> s = new Stack<elemtype>(); 
	        int m,low,high;
	        elemtype E=new elemtype ();
	        low=0;high=n-1;
	        E.high=n-1;
	        E.low=0;
            s.Push(E); 
	        while(s.Count!=0)
	        {
                E = new elemtype();
                E = s.Pop(); 
		        high=E.high;
		        low=E.low;
		        m=QSwap(A,low,high);
		        if(m>0)
		    	{
                    E = new elemtype();
			        E.low=low;						//支点左边数据位置进栈
			        E.high=m-1;
                    s.Push(E);

                    E = new elemtype();
			        E.low =m+1;						//支点右边数据位置进栈
			        E.high=high;
                    s.Push(E); 
			    }
	         }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            int i,m;
	        int []a={49,38,65,97,76,13,27,49};		//快速排序测试数据
	        SQSort(a,8);
              listBox1.Items.Clear();
            for (i = 0; i < 8; i++)
                listBox1.Items.Add(a[i].ToString());
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
发布了331 篇原创文章 · 获赞 340 · 访问量 175万+

猜你喜欢

转载自blog.csdn.net/lucky51222/article/details/104392763
今日推荐