1.冒泡排序(Bubble sort)
基本的
View Code
1 void BubbleSort(int A[],int n) 2 { 3 int i,j; 4 for(i=1;i<n;i++) //最多做n-1趟排序 5 for(j=n-1;j>=i;j--) //从后往前冒泡,最"轻"的冒泡到最前面 6 if(A[j]<A[j-1]) 7 swap(A[j],A[j-1]); 8 }
改进的
View Code
1 void BubbleSort_O(int A[],int n) 2 { 3 int i,j; 4 for(i=1;i<n;i++){ //最多做n-1趟排序 5 bool exchange=false; //交换标志 6 for(j=n-1;j>=i;j--) //从后往前冒泡,最"轻"的冒泡到最前面 7 if(A[j]<A[j-1]){ 8 swap(A[j],A[j-1]); 9 exchange=true; 10 } 11 if(!exchange) //本趟排序未发生交换,提前终止算法 12 break; 13 } 14 }
View Code
1 void SelectionSort(int A[],int n) 2 { 3 int i,j; 4 for(i=0;i<n-1;i++){ 5 int min_index=i; 6 for(j=i+1;j<n;j++) 7 if(A[j]<A[min_index]) 8 min_index=j; 9 if(min_index!=i) 10 swap(A[i],A[min_index]); 11 } 12 }
View Code
1 void InsertSort(int A[], int n) 2 { 3 int i,j; 4 for(i=1;i<n;i++) //依次插入R[1],…,R[n-1] 5 if(A[i]<A[i-1]){ 6 int insert_value=A[i]; 7 j=i-1; 8 do { //从右向左在有序区R[0..i-1]中查找A[i]的插入位置 9 A[j+1]=A[j]; //将关键字大于A[i]的记录后移 10 j--; 11 }while(j>=0 && A[j]>insert_value); //当A[j]<=A[i]时终止 12 A[j+1]=insert_value; //A[i]插入到正确的位置上 13 } 14 }
View Code
1 void ShellInsert(int A[],int n,int d) 2 {//希尔排序中的一趟排序,d为当前增量 3 int i,j; 4 for(i=d;i<n;i++) //将R[d..n-1]分别插入各组当前的有序区 5 if(A[i]<A[i-d]){ 6 int insert_value=A[i]; 7 j=i-d; 8 do {//查找A[i]的插入位置 9 A[j+d]=A[j]; //后移记录 10 j=j-d; //查找前一记录 11 }while(j>=0 && A[j]>insert_value); 12 A[j+d]=insert_value; //插入A[i]到正确的位置上 13 } //endif 14 } //ShellPass 15 16 void ShellSort(int A[],int n) 17 { 18 int increment=n/2+(n/2%2?0:1); //增量初值,这里初始化为一个靠近n的一半的奇数 19 while(increment>0){ 20 ShellInsert(A,n,increment); //一趟增量为increment的Shell插入排序 21 increment-=2; //求下一增量 22 } 23 }
View Code
1 int Partition(int A[],int i,int j) 2 { 3 int pivot=A[i];//中轴 4 while(i<j){ 5 //把右半边小于中轴的记录移到左半边 6 while(i<j && A[j]>=pivot) j--; 7 if(i<j) A[i++]=A[j]; 8 //把左半边大于中轴的记录移到右半边 9 while(i<j && A[i]<=pivot) i++; 10 if(i<j) A[j--]=A[i]; 11 } 12 A[i]=pivot; 13 return i; 14 } 15 16 void QuickSort(int A[],int low,int high) 17 { 18 if(low>=high) return; 19 int pivotpos=Partition(A,low,high); 20 QuickSort(A,low,pivotpos-1); 21 QuickSort(A,pivotpos+1,high); 22 }
View Code
1 void Heapify(int A[], int i, int HeapSize) 2 { 3 int left = LEFT(i); 4 int right = RIGHT(i); 5 int largest=i; 6 7 if (left<=HeapSize && A[largest]<A[left]) 8 largest = left; 9 10 if (right<=HeapSize && A[largest]<A[right]) 11 largest = right; 12 13 if (largest!=i) 14 { 15 swap(A[largest], A[i]); 16 Heapify(A, largest, HeapSize); //调整后可能仍然违反堆性质(ps.也可以用非递归的方式) 17 } 18 } 19 20 void BuildHeap(int A[], int n) 21 { 22 for(int i=PARENT(n);i>=0;i--)//从最后一个非叶节点开始,往前逐个调整到根节点 23 Heapify(A, i, n); 24 } 25 26 void HeapSort(int A[], int n) 27 { 28 BuildHeap(A,n); 29 for (int i=n-1;i>0;i--) 30 { 31 swap(A[0], A[i]); 32 Heapify(A,0,i-1); 33 } 34 }
View Code
1 void Merge(int a[], int b[], int low, int mid, int high) 2 { 3 int k = low; 4 int begin1 = low; 5 int end1 = mid; 6 int begin2 = mid + 1; 7 int end2 = high; 8 while(k <= high ) 9 { 10 if(begin1 > end1) 11 b[k++] = a[begin2++]; 12 else if(begin2 > end2) 13 b[k++] = a[begin1++]; 14 else 15 { 16 if(a[begin1] <= a[begin2]) 17 b[k++] = a[begin1++]; 18 else 19 b[k++] = a[begin2++]; 20 } 21 } 22 } 23 24 void MergePass(int a[], int b[], int seg, int size) 25 { 26 int seg_start_ind = 0; 27 while(seg_start_ind + 2 * seg <= size) //#size - 2 * seg的意思是滿足可兩兩歸併的最低臨界值 28 { 29 Merge(a, b, seg_start_ind, seg_start_ind + seg - 1, seg_start_ind + seg * 2 - 1); 30 seg_start_ind += 2 * seg; 31 } 32 //如果一段是正好可歸併的數量而另一段則少於正好可歸併的數量 33 if(seg_start_ind + seg < size) 34 Merge(a, b, seg_start_ind, seg_start_ind + seg - 1, size - 1); 35 else 36 for(int j = seg_start_ind; j < size; j++) //如果只剩下一段或者更少的數量 37 b[j] = a[j]; 38 } 39 40 void MergeSort(int a[], int size) 41 { 42 int* temp = new int[size]; 43 int seg = 1; 44 while(seg < size) 45 { 46 MergePass(a, temp, seg, size); 47 seg = seg*2; 48 MergePass(temp, a, seg, size); 49 seg = seg*2; 50 } 51 delete[] temp; 52 }
View Code
1 #include<iostream> 2 #include<vector> 3 #include<iterator> 4 #include<algorithm> 5 6 typedef std::vector<unsigned int> input_type; 7 8 void radix_sort(input_type & x) 9 { 10 if ( x.empty() ) return; // at least one element 11 12 typedef std::vector< std::vector< input_type::value_type > > buckets_type; 13 buckets_type buckets(10); // allocate buckets 14 // for sorting decimal numbers 15 16 int pow10 = 1; // pow10 holds powers of 10 (1, 10, 100, ...) 17 18 // find maximum in the array to limit the main loop below 19 input_type::value_type max = *std::max_element(x.begin(), x.end()); 20 21 //begin radix sort 22 for(; max != 0 ; max/=10, pow10*=10) 23 { 24 // 1. determine which bucket each element should enter 25 // for each element in 'x': 26 for(input_type::const_iterator elem = x.begin(); elem != x.end(); ++elem) 27 { 28 // calculate the bucket number: 29 size_t const bucket_num = ( *elem / pow10 ) % 10; 30 // add the element to the list in the bucket: 31 buckets[ bucket_num ].push_back( *elem ); 32 } 33 34 // 2. transfer results of buckets back into main array 35 input_type::iterator store_pos = x.begin(); 36 // for each bucket: 37 for(buckets_type::iterator bucket = buckets.begin(); bucket != buckets.end(); ++bucket) 38 { 39 // for each element in the bucket: 40 for(buckets_type::value_type::const_iterator bucket_elem = bucket->begin(); 41 bucket_elem != bucket->end(); ++bucket_elem) 42 { 43 // copy the element into next position in the main array 44 *store_pos++ = *bucket_elem; 45 } 46 bucket->clear(); // forget the current bucket's list 47 } 48 } 49 } 50 51 int main(){ 52 53 input_type input; 54 55 // read numbers from standard input (ends with end-of-file: ^Z / ^D, or 56 // with a new line that contains something that's not a number) 57 std::cout << "Enter positive numbers to sort:" << std::endl; 58 std::copy(std::istream_iterator<unsigned int>(std::cin), 59 std::istream_iterator<unsigned int>(), std::back_inserter(input)); 60 61 if ( input.end() != std::find(input.begin(), input.end(), 0) ) 62 { 63 std::cerr << "Zero isn't positive" << std::endl; 64 return 1; 65 } 66 67 std::cout << " ** Elements before sorting: " << std::endl; 68 std::copy(input.begin(), input.end(), 69 std::ostream_iterator<unsigned int>(std::cout, " ")); 70 71 radix_sort(input); 72 73 std::cout << std::endl << " ** Elements after sorting: " << std::endl; 74 std::copy(input.begin(), input.end(), 75 std::ostream_iterator<unsigned int>(std::cout, " ")); 76 std::cout << std::endl; 77 78 return 0; 79 }
参考:http://student.zjzk.cn/course_ware/data_structure/web/paixu/paixu8.3.2.4.htm
http://www.cnblogs.com/kkun/archive/2011/11/23/2260312.html
http://blog.csdn.net/deutschester/article/details/5946299