排序代码合集(冒泡排序、插入排序、选择排序、快速排序、计数排序、)
#include <iostream>
using namespace std;
/*
bubble sort
param:
@seq: A array that will be sorted.
@l: begin index include itself.
@r: end index expect itself.
@compare: compared function.
*/
void bubble(int seq[], int l, int r, int (*compare)(int, int)){
/*1st method: Keep the subseq is sequential since two elements.
**@cite: Xiaodong Wang <<数据结构与算法>>
for(int i = l+1; i<=r; i++){
for(int j = i;j>l; j--){
if(compare(seq[j-1], seq[j])){
seq[j-1] ^= seq[j];
seq[j] ^= seq[j-1];
seq[j-1] ^= seq[j];
}
}
}
*/
// 2nd method: Bubble a extreme element each loop.
for(int i = l; i<=r; i++){
for(int j = l;j<=r-i; j++){
if(compare(seq[j-1], seq[j])){
seq[j-1] ^= seq[j];
seq[j] ^= seq[j-1];
seq[j-1] ^= seq[j];
}
}
}
}
/*
insert sort
*/
void insertion(int seq[], int l, int r, int (*compare)(int, int)){
int i;
for(i = l+1;i<=r;i++){
int v = seq[i];// save source element.
int temp = i;
// search the inserted position.
while(temp>l&& compare(seq[temp-1], v)){
seq[temp] = seq[temp-1];
temp--;
}
seq[temp] = v;
}
}
/*
select sort
*/
void selection(int seq[], int l, int r, int (*compare)(int, int)){
for(int i=l;i<r;i++){
int temp,min = i;
for(temp = i+1;temp<=r;temp++){
if(compare(seq[min], seq[temp])){
min = temp;// save the index of min element.
}
}
swap(seq[i],seq[min]);// exchange elements.
}
}
/*
quicksort
return:
index of dividing element.
*/
int partition(int seq[], int l, int r, int (*compare)(int, int)){
//r is the index of dividing line.
int begin = l, end = r-1, v = seq[r];
while(1){
while(begin<=r && compare(v, seq[begin++])); //search the first element that bigger than V from head.
while(end>=l && compare(seq[end--], v)); //search the first element that smaller than V from end.
if(begin-1 >= end+1){// searching over.
break;
}
swap(seq[begin-1],seq[end+1]); // swap the two elements of index of named begin and end.
}
swap(seq[begin-1],seq[r]);// swap the dividing element and the element of begin index.
return begin-1;
}
void quicksort(int seq[], int l, int r, int (*compare)(int, int)){
if(r <=l){
return;
}
int i = partition(seq,l,r,compare);
quicksort(seq,l,i-1,compare);
quicksort(seq,i+1,r,compare);
}
/*
count sort
@param:
seq: input array.
sortSeq: result of sorting.
n: a number of elements.
*/
#define m 10000
int* countsort(int seq[], int n){
int sortSeq[100];
int c[m+1];
for(int i = 0;i<m;i++){
c[i] = 0;
}
// count the number of every element.
for(int i = 0;i<n;i++){
c[seq[i]]++;
}
//count the number of element which smaller itself. {5,1,12,10,11,3,11}
for(int i = 1;i<m;i++){
c[i] += c[i-1];
}
for(int i = 0;i<n;i++){
sortSeq[(c[seq[i]]-1)] = seq[i];
c[seq[i]]--;
}
return sortSeq;
}
/*
self-defining function
*/
int myCompare(int a,int b){
if(a>b){
return 1;
}else{
return 0;
}
}
int main(){
int seq[] = {6,1,4,5,2,3,6};
int seq2[] = {6,1,4,10,25,13,11};
int seq3[] = {6,1,77,5,66,3,6};
int seq4[] = {22,1,75,10,11,33,11};
int seq5[] = {5,1,12,10,11,3,11};
//--------------bubble
bubble(seq,0,6,myCompare);
for(int i = 0;i<6;i++){
cout<<seq[i]<<"<";
}
cout<<seq[6]<<"--bubble"<<endl;
//--------------insertion
insertion(seq2,0,6,myCompare);
for(int i = 0;i<6;i++){
cout<<seq2[i]<<"<";
}
cout<<seq2[6]<<"--insertion"<<endl;
//--------------selection
selection(seq3,0,6,myCompare);
for(int i = 0;i<6;i++){
cout<<seq3[i]<<"<";
}
cout<<seq3[6]<<"--selection"<<endl;
//--------------quicksort
quicksort(seq4,0,6,myCompare);
for(int i = 0;i<6;i++){
cout<<seq4[i]<<"<";
}
cout<<seq4[6]<<"--quicksort"<<endl;
//--------------countsort
int* seqP = countsort(seq5,7);
for(int i = 0;i<6;i++){
cout<<seqP[i]<<"<";
}
cout<<seqP[6]<<"--countsort"<<endl;
return 0;
}