zoukankan      html  css  js  c++  java
  • 八排序算法



        

        nO(nlog2n)

       keyword


     

    1.(Straight Insertion Sort)

    :

    112





    void print(int a[], int n ,int i){
    	cout<<i <<":";
    	for(int j= 0; j<8; j++){
    		cout<<a[j] <<" ";
    	}
    	cout<<endl;
    }
    
    
    void InsertSort(int a[], int n)
    {
    	for(int i= 1; i<n; i++){
    		if(a[i] < a[i-1]){               //ii-1
    			int j= i-1;	
    			int x = a[i];		 //
    			a[i] = a[i-1];           //
    			while(x < a[j]){	 //
    				a[j+1] = a[j];
    				j--;		 //
    			}
    			a[j+1] = x;		 //
    		}
    		print(a,n,i);			//
    	}
    	
    }
    
    int main(){
    	int a[8] = {3,1,5,7,2,4,9,6};
    	InsertSort(a,8);
    	print(a,8,8);
    }
    

    On^2.

    2-

     2. Shell`s Sort

    1959 D.L.Shell


    1. t1t2tkti>tjtk=1
    2. kk
    3. tim 1


    d = {n/2 ,n/4, n/8 .....1} n

    dn/2,nd.d/21

    void print(int a[], int n ,int i){
    	cout<<i <<":";
    	for(int j= 0; j<8; j++){
    		cout<<a[j] <<" ";
    	}
    	cout<<endl;
    }
    /**
     * 
     *
     * @param int dk dk=1
     *
     */
    
    void ShellInsertSort(int a[], int n, int dk)
    {
    	for(int i= dk; i<n; ++i){
    		if(a[i] < a[i-dk]){			//ii-1
    			int j = i-dk;	
    			int x = a[i];			//
    			a[i] = a[i-dk];			//
    			while(x < a[j]){		//
    				a[j+dk] = a[j];
    				j -= dk;			 //
    			}
    			a[j+dk] = x;			//
    		}
    		print(a, n,i );
    	}
    	
    }
    
    /**
     * dn/2,n
     *
     */
    void shellSort(int a[], int n){
    
    	int dk = n/2;
    	while( dk >= 1  ){
    		ShellInsertSort(a, n, dk);
    		dk = dk/2;
    	}
    }
    int main(){
    	int a[8] = {3,1,5,7,2,4,9,6};
    	//ShellInsertSort(a,8,1); //
    	shellSort(a,8);			  //
    	print(a,8,8);
    }
    

    d1 1


    3. Simple Selection Sort

    12n-1n


     

    n

    n-1

    .....

    i i n-i+1 i




    void print(int a[], int n ,int i){
    	cout<<""<<i+1 <<" : ";
    	for(int j= 0; j<8; j++){
    		cout<<a[j] <<"  ";
    	}
    	cout<<endl;
    }
    /**
     * 值
     *
     * @return int 值
     */
    int SelectMinKey(int a[], int n, int i)
    {
    	int k = i;
    	for(int j=i+1 ;j< n; ++j) {
    		if(a[k] > a[j]) k = j;
    	}
    	return k;
    }
    
    /**
     * 
     *
     */
    void selectSort(int a[], int n){
    	int key, tmp;
    	for(int i = 0; i< n; ++i) {
    		key = SelectMinKey(a, n,i);           //
    		if(key != i){
    			tmp = a[i];  a[i] = a[key]; a[key] = tmp; //i
    		}
    		print(a,  n , i);
    	}
    }
    int main(){
    	int a[8] = {3,1,5,7,2,4,9,6};
    	cout<<"值";
    	for(int j= 0; j<8; j++){
    		cout<<a[j] <<"  ";
    	}
    	cout<<endl<<endl;
    	selectSort(a, 8);
    	print(a,8,8);
    }
    

     

    ,n[n/2]

    void SelectSort(int r[],int n) {
    	int i ,j , min ,max, tmp;
    	for (i=1 ;i <= n/2;i++) {  
    		// n/2 
    		min = i; max = i ; //keyword
    		for (j= i+1; j<= n-i; j++) {
    			if (r[j] > r[max]) { 
    				max = j ; continue ; 
    			}  
    			if (r[j]< r[min]) { 
    				min = j ; 
    			}   
    	  }  
    	  //
    	  tmp = r[i-1]; r[i-1] = r[min]; r[min] = tmp;
    	  tmp = r[n-i]; r[n-i] = r[max]; r[max] = tmp; 
    
    	} 
    }

    4. Heap Sort


    nk1,k2,...,kn),



    值()值值()

    a96, 83,27,38,11,09)

      (b)  1236248547305391



    nn ()(n-1)n ()n


    1. n
    2. n-1


    n-1

    1m m-1

    2

    3 2.

    4 2.

    5



    n

    1n

    2

    3

    4938659776132749
                                 


                                 

     

    void print(int a[], int n){
    	for(int j= 0; j<n; j++){
    		cout<<a[j] <<"  ";
    	}
    	cout<<endl;
    }
    
    
    
    /**
     * H[sm]H[s] 
     * H[s],.s, 
     *
     * @param H
     * @param s
     * @param length
     *
     */
    void HeapAdjust(int H[],int s, int length)
    {
    	int tmp  = H[s];
    	int child = 2*s+1; //(i+1 )
        while (child < length) {
    		if(child+1 <length && H[child]<H[child+1]) { // ()
    			++child ;
    		}
    		if(H[s]<H[child]) {  // 
    			H[s] = H[child]; // 
    			s = child;		 // s ,
    			child = 2*s+1;
    		}  else {			 // 
    			 break;
    		}
    		H[s] = tmp;			// 
    	}
    	print(H,length);
    }
    
    
    /**
     * 
     * H[0..length-1]
     * 
     */
    void BuildingHeap(int H[], int length)
    { 
    	// i=  (length -1) / 2
    	for (int i = (length -1) / 2 ; i >= 0; --i)
    		HeapAdjust(H,i,length);
    }
    /**
     * 
     */
    void HeapSort(int H[],int length)
    {
        //
    	BuildingHeap(H, length);
    	//
    	for (int i = length - 1; i > 0; --i)
    	{
    		//H[0]
    		int temp = H[i]; H[i] = H[0]; H[0] = temp;
    		//
    		HeapAdjust(H,0,i);
      }
    } 
    
    int main(){
    	int H[10] = {3,1,5,7,2,4,9,6,10,8};
    	cout<<"值";
    	print(H,10);
    	HeapSort(H,10);
    	//selectSort(a, 8);
    	cout<<"";
    	print(H,10);
    
    }
    
    


    :

    k2(k-1)k  

                                   

    4n O(nlogn )

    5. Bubble Sort

     

    void bubbleSort(int a[], int n){
    	for(int i =0 ; i< n-1; ++i) {
    		for(int j = 0; j < n-i-1; ++j) {
    			if(a[j] > a[j+1])
    			{
    				int tmp = a[j] ; a[j] = a[j+1] ;  a[j+1] = tmp;
    			}
    		}
    	}
    }


    exchange

    1pos,pos,pos

    :

    void Bubble_1 ( int r[], int n) {
    	int i= n -1;  //,
    	while ( i> 0) { 
    		int pos= 0; //,
    		for (int j= 0; j< i; j++)
    			if (r[j]> r[j+1]) {
    				pos= j; // 
    				int tmp = r[j]; r[j]=r[j+1];r[j+1]=tmp;
    			} 
    		i= pos; //
    	 } 
    }  
    

    2值值,() ,

    :

    void Bubble_2 ( int r[], int n){
    	int low = 0; 
    	int high= n -1; //值
    	int tmp,j;
    	while (low < high) {
    		for (j= low; j< high; ++j) //,
    			if (r[j]> r[j+1]) {
    				tmp = r[j]; r[j]=r[j+1];r[j+1]=tmp;
    			} 
    		--high;					//high值, 
    		for ( j=high; j>low; --j) //,
    			if (r[j]<r[j-1]) {
    				tmp = r[j]; r[j]=r[j-1];r[j-1]=tmp;
    			}
    		++low;					//low值,
    	} 
    } 

    6. Quick Sort

    1,,

    2值值 值值

    3

    4

    a

    b


    void print(int a[], int n){
    	for(int j= 0; j<n; j++){
    		cout<<a[j] <<"  ";
    	}
    	cout<<endl;
    }
    
    void swap(int *a, int *b)
    {
    	int tmp = *a;
    	*a = *b;
    	*b = tmp;
    }
    
    int partition(int a[], int low, int high)
    {
    	int privotKey = a[low];								//
    	while(low < high){								    //
    		while(low < high  && a[high] >= privotKey) --high;  //high low+1 
    		swap(&a[low], &a[high]);
    		while(low < high  && a[low] <= privotKey ) ++low;
    		swap(&a[low], &a[high]);
    	}
    	print(a,10);
    	return low;
    }
    
    
    void quickSort(int a[], int low, int high){
    	if(low < high){
    		int privotLoc = partition(a,  low,  high);  //
    		quickSort(a,  low,  privotLoc -1);			//
    		quickSort(a,   privotLoc + 1, high);		//
    	}
    }
    
    int main(){
    	int a[10] = {3,1,5,7,2,4,9,6,10,8};
    	cout<<"值";
    	print(a,10);
    	quickSort(a,0,9);
    	cout<<"";
    	print(a,10);
    
    }


    O(nlog2n)

     

    ,k,k 8 ,

    void print(int a[], int n){
    	for(int j= 0; j<n; j++){
    		cout<<a[j] <<"  ";
    	}
    	cout<<endl;
    }
    
    void swap(int *a, int *b)
    {
    	int tmp = *a;
    	*a = *b;
    	*b = tmp;
    }
    
    int partition(int a[], int low, int high)
    {
    	int privotKey = a[low];					//
    	while(low < high){					//
    		while(low < high  && a[high] >= privotKey) --high; //high low+1 
    		swap(&a[low], &a[high]);
    		while(low < high  && a[low] <= privotKey ) ++low;
    		swap(&a[low], &a[high]);
    	}
    	print(a,10);
    	return low;
    }
    
    
    void qsort_improve(int r[ ],int low,int high, int k){
    	if( high -low > k ) { //k, k
    		int pivot = partition(r, low, high); // Partition
    		qsort_improve(r, low, pivot - 1,k);
    		qsort_improve(r, pivot + 1, high,k);
    	} 
    } 
    void quickSort(int r[], int n, int k){
    	qsort_improve(r,0,n,k);//Qsort
    
    	//
    	for(int i=1; i<=n;i ++){
    		int tmp = r[i]; 
    		int j=i-1;
    		while(tmp < r[j]){
    			r[j+1]=r[j]; j=j-1; 
    		}
    		r[j+1] = tmp;
    	} 
    
    } 
    
    
    
    int main(){
    	int a[10] = {3,1,5,7,2,4,9,6,10,8};
    	cout<<"值";
    	print(a,10);
    	quickSort(a,9,4);
    	cout<<"";
    	print(a,10);
    
    }


    7. Merge Sort


    Merge

     


    r[in]r[im]r[m+1n]n-i +1n-m

    1. j=m+1k=ii=i; //
    2. i>m j>n //
    3. //r[i]r[j]rf
      r[i]<r[j]rf[k]=r[i] i++ k++
      rf[k]=r[j] j++ k++
    4. //rf
      i<=mr[im]rf[kn] //
      j<=n ,  r[jn] rf[kn] //
    //r[im]r[m +1 n]rf[in]
    void Merge(ElemType *r,ElemType *rf, int i, int m, int n)
    {
    	int j,k;
    	for(j=m+1,k=i; i<=m && j <=n ; ++k){
    		if(r[j] < r[i]) rf[k] = r[j++];
    		else rf[k] = r[i++];
    	}
    	while(i <= m)  rf[k++] = r[i++];
    	while(j <= n)  rf[k++] = r[j++];
    }



    1 n 1 n/21 2n

    void print(int a[], int n){
    	for(int j= 0; j<n; j++){
    		cout<<a[j] <<"  ";
    	}
    	cout<<endl;
    }
    
    //r[im]r[m +1 n]rf[in]
    void Merge(ElemType *r,ElemType *rf, int i, int m, int n)
    {
    	int j,k;
    	for(j=m+1,k=i; i<=m && j <=n ; ++k){
    		if(r[j] < r[i]) rf[k] = r[j++];
    		else rf[k] = r[i++];
    	}
    	while(i <= m)  rf[k++] = r[i++];
    	while(j <= n)  rf[k++] = r[j++];
    	print(rf,n+1);
    }
    
    void MergeSort(ElemType *r, ElemType *rf, int lenght)
    { 
    	int len = 1;
    	ElemType *q = r ;
    	ElemType *tmp ;
    	while(len < lenght) {
    		int s = len;
    		len = 2 * s ;
    		int i = 0;
    		while(i+ len <lenght){
    			Merge(q, rf,  i, i+ s-1, i+ len-1 ); //
    			i = i+ len;
    		}
    		if(i + s < lenght){
    			Merge(q, rf,  i, i+ s -1, lenght -1); //
    		}
    		tmp = q; q = rf; rf = tmp; //q,rfq rf
    	}
    }
    
    
    int main(){
    	int a[10] = {3,1,5,7,2,4,9,6,10,8};
    	int b[10];
    	MergeSort(a, b, 10);
    	print(b,10);
    	cout<<"";
    	print(a,10);
    
    }

    void MSort(ElemType *r, ElemType *rf,int s, int t)
    { 
    	ElemType *rf2;
    	if(s==t) r[s] = rf[s];
    	else
    	{ 
    		int m=(s+t)/2;			/**p */
    		MSort(r, rf2, s, m);		/*p[sm]p2[sm]*/
    		MSort(r, rf2, m+1, t);		/*p[m+1t]p2[m+1t]*/
    		Merge(rf2, rf, s, m+1,t);	/*p2[sm]p2[m+1t]p1[st]*/
    	}
    }
    void MergeSort_recursive(ElemType *r, ElemType *rf, int n)
    {   /**p */
    	MSort(r, rf,0, n-1);
    }

    8. /(Radix Sort)

    值n O(n log n)
              

     [1..1000]nA[1..n]  

     10B[1][1..10]B[2]   (10..20]B[i](   (i-1)*10,   i*10]i   =   1,2,..100  100  

      A[1..n]A[i]B[j]  100 

         

      nmn/m  

       

      O(n   +   m   *   n/m*log(n/m))   =   O(n   +   nlogn   -   nlogm)  

      mnO(n)  

      n    

            On2O(nlogn)On

            1值0m-1mm

            2

          


    keywordO(n)

    :

    52 值
    < < <  
    2 < 3 < 4 < 5 < 6 < 7 < 8 < 9 < 10 < J < Q < K < A



    值值


    14 值4
    213 值13 (2 3 ...A )值13 4 ()23 4 值4

    n d {k1k2kd}{k1k2kd}r[i]r[j](1ijn)

                                                                  

    k1 kd      

    (Most Significant Digit first)MSD

    1k1 k1

    2k2 kd

    3值MSD

    (Least Significant Digit first)LSD

    1) kd kd-1k1

    2) , 值LSD


    LSD

    keywordkeywordkeywordkeyword-值4值13值4值13   

    :

    Void RadixSort(Node L[],length,maxradix)
    {
       int m,n,k,lsp;
       k=1;m=1;
       int temp[10][length-1];
       Empty(temp); //
       while(k<maxradix) //keyword
       {
         for(int i=0;i<length;i++) //
        {
           if(L[i]<m)
              Temp[0][n]=L[i];
           else
              Lsp=(L[i]/m)%10; //keyword
           Temp[lsp][n]=L[i];
           n++;
       }
       CollectElement(L,Temp); //
       n=0;
       m=m*10;
      k++;
     }
    }








                                 O(n)


    nO(nlog2n)


    (1)(O(n2))
    :
     (2)(O(nlog2n))

     (3)O(n1+)),01

          
    (4)(O(n))

    On

    On2

     

    :keyword  
        

    1n

    2keyword

    3keyword

    4

    n.

    1nO(nlog2n)

       keyword
            

          

    2  n =

    3n

       

       

    5

    6

    1keyword

    2
    keyword
    3

    http://blog.csdn.net/hguisu/article/details/7776068
  • 相关阅读:
    TopCoder12729 「SRM589Medium」GearsDiv1 二分图最小点覆盖
    Codechef RIN 「Codechef14DEC」Course Selection 最小割离散变量模型
    BZOJ3144/LG3227 「HNOI2013」切糕 最小割离散变量模型
    20191214 Codeforces Round #606 (Div. 2, based on Technocup 2020 Elimination Round 4)
    [Codeforces868F]Yet Another Minimization Problem
    2020年计算机算法基础理论与应用研讨会小记
    [Codeforces1421E]Swedish Heroes
    [Codechef CCC]Hit the coconuts
    CSP-S 2020
    牛客NOIP2020模拟
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/4557717.html
Copyright © 2011-2022 走看看