Sorting Algorithm Summary-code + performance

Source: Internet
Author: User

Sorting Algorithm Summary-code + performance

// Data_sort_alg.cpp: defines the entry point of the console application. // # Include "stdafx. h" # include "sort_alg.h" # include
 
  
# Include
  
   
Void show (std: vector
   
    
& A) {std: vector
    
     
: Iterator it = a. begin (); while (it! = A. end () {std: cout <* it <""; ++ it;} std: cout <
     
      
Src (a, a + 11); std: vector
      
        Src1 (B, B + 12); // heap sortstd: vector
       
         Src2 (c, c + 11); // bucket sortstd: vector
        
          Src3 (d, d + 11); // radix sortstd: cout <"The original data order is:" <
         <<"the sorting result is:"<
          
#ifndef SORT_ALG_H#define SORT_ALG_H#include 
           
            void bubble_sort(std::vector
            
              &a);void shell_sort(std::vector
             
               &a);void simple_sort(std::vector
              
                &a);void merge_sort(std::vector
               
                 &s,int b,int e);void insert_sort(std::vector
                
                 & a);void heap_sort(std::vector
                 
                   &a);void fast_sort(std::vector
                  
                    &a,int start,int end);void bucket_sort(std::vector
                   
                     &a);void radix_sort(std::vector
                    
                      &a);#endif
                    
                   
                  
                 
                
               
              
             
            
           
#include "stdafx.h"#include 
           
            #include#define STEP 5/*==============================1 name:bubble sort--------------------------------time complexity:worstaveragebestO(n^2)O(n^2)O(n)--------------------------------space complexity:O(1)--------------------------------stability:stable==============================*/void bubble_sort(std::vector
            
              &a){for(int i=a.size()-1;i>0;i--){for(int j=0;j
             
              a[j+1]){std::swap(a[j],a[j+1]);}}}return;}/*==============================2 name:shell sort--------------------------------time complexity:worstaveragebestO(ns)O(nlogn)O(n)1
              <2--------------------------------space complexity:o(1)--------------------------------stability:unstable="=============================*/void" shell_sort_assist(std::vector
                &a,int d){int i=d;while(i=j){a[k]=a[k-d];k=k-d;}a[j]=temp;break;}j=j+d;}i=i+d;}return;}void shell_sort(std::vector
               
                 &a){int d=STEP;while(d!=0){shell_sort_assist(a,d);d/=2;}return;}/*==============================3 name:simple sort--------------------------------time complexity:worstaveragebestO(n^2)O(n^2)O(n^2)--------------------------------space complexity:O(1)--------------------------------stability:unstable==============================*/void simple_sort(std::vector
                
                  &a){std::vector
                 
                  ::iterator it=a.begin(),it1,saveit;for(;it!=a.end();it++){int min=*it;saveit=it;it1=it;while(it1!=a.end()){if(min>*it1){min=*it1;saveit=it1;}it1++;}if(saveit!=it){std::swap(*it,*saveit);}}return;}/*==============================4 name: merge sort--------------------------------time complexity:worstaveragebestO(nlogn)O(nlogn)O(nlogn)--------------------------------space complexity:O(n)--------------------------------stability:stable==============================*/void merge(std::vector
                  
                    &s,int b,int m,int e){int* temp=new int[e-b+1];int i=b;int j=m+1;int k=0;while(i
                   
                    s[j]){temp[k++]=s[j++];}else{temp[k++]=s[i++];}}if(i
                    
                      &s,int b,int e){int m;if(e>b){m=(b+e)/2;merge_sort(s,b,m);merge_sort(s,m+1,e);merge(s,b,m,e);}return;}/*==============================5 name: insert sort--------------------------------time complexity:worstaveragebestO(n^2)O(n^2)O(n)--------------------------------space complexity:O(1)--------------------------------stability:stable==============================*/void insert_sort(std::vector
                     
                      & a){std::vector
                      
                       ::iterator it,it1,it2;int temp;it=a.begin();while(it!=a.end()){it1=a.begin();while(it1!=it){if(*it<*it1){temp=*it;it2=it;while(it2!=it1){*it2=*(it2-1);it2--;}*it1=temp;break;}it1++;}it++;}}/*==============================6 name: heap sort--------------------------------time complexity:worstaveragebestO(nlogn)O(nlogn)O(nlogn)--------------------------------space complexity:O(1)--------------------------------stability:unstable==============================*/void heap_adjust(std::vector
                       
                         &a,int i,int end){int lchild=2*i;int rchild=2*i+1;int max=i;if(rchild
                        
                         a[max]){max=lchild;}if(a[rchild]>a[max]){max=rchild;}if(max!=i){std::swap(a[max],a[i]);heap_adjust(a,max,end);}}}void build_heap(std::vector
                         
                           &a){int size=a.size()-1;for(int i=size/2;i>0;i--){heap_adjust(a,i,size);}return;}void heap_sort(std::vector
                          
                            &a){build_heap(a);for(int i=a.size()-1;i>0;i--){std::swap(a[1],a[i]);heap_adjust(a,1,i-1);}}/*==============================7 name: fast sort--------------------------------time complexity:worstaveragebestO(n^2)O(nlogn)O(nlogn)--------------------------------space complexity:O(logn)--------------------------------stability:unstable==============================*/void fast_sort(std::vector
                           
                             &a,int start,int end){int med=a[start];int i=start;int j=end;while(j>i){while(a[j]>=med && j>i){j--;}a[i]=a[j];while(a[i]<=med && j>i){i++;}a[j]=a[i];}a[i]=med;if(start
                            
                              &a){std::vector
                             
                              > bucket;bucket.resize(10);std::vector
                              
                               ::iterator it=a.begin();while(it!=a.end()){int idx=*it/10;bucket[idx].push_back(*it);it++;}std::vector
                               
                                >::iterator it1=bucket.begin();while(it1!=bucket.end()){simple_sort(*it1);it1++;}it=a.begin();it1=bucket.begin();while(it!=a.end() && it1!=bucket.end()){std::vector
                                
                                 ::iterator tmp_it=(*it1).begin();while(tmp_it!=(*it1).end()){*it=*tmp_it;tmp_it++;it++;}it1++;}}/*==============================9 name: radix sort--------------------------------time complexity:averageO(2dn)--------------------------------space complexity:O(n)--------------------------------stability:stable==============================*/void refresh_data(std::vector
                                 
                                   &a, std::vector
                                  
                                   > &sto){std::vector
                                   
                                    ::iterator it,it1;std::vector
                                    
                                     >::iterator it2;it=a.begin();it2=sto.begin();while(it!=a.end() && it2!=sto.end()){it1=(*it2).begin();while(it1!=(*it2).end()){*it=*it1;it1++;it++;}(*it2).clear();it2++;}return;}//suppose:there are no minus numbervoid radix_sort(std::vector
                                     
                                       &a){std::vector
                                      
                                       > sto;sto.resize(10);int max=0;std::vector
                                       
                                        ::iterator it=a.begin();while(it!=a.end()){int idx;if(max<*it){max=*it;}idx=(*it)%10;sto[idx].push_back(*it);it++;}refresh_data(a,sto);int d=0;int temp=max;while(1){if(temp!=0){d++;}else{break;}temp/=10;}for(int i=1;i<=d;i++){int div=pow(10.0,i);it=a.begin();while(it!=a.end()){int idx;idx=(*it/div)%10;sto[idx].push_back(*it);it++;}refresh_data(a,sto);}return;}
                                       
                                      
                                     
                                    
                                   
                                  
                                 
                                
                               
                              
                             
                            
                           
                          
                         
                        
                       
                      
                     
                    
                   
                  
                 
                
               
              <2--------------------------------space>
             
            
           



<<"the>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.