Last active
January 22, 2018 13:11
-
-
Save tamarous/fad718b1382c12e700291b5ce4a9b765 to your computer and use it in GitHub Desktop.
快速排序和冒泡排序
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void bubbleSort(int array[],int n) { | |
int i,j,tmp; | |
for(i = 0;i < n;i++) { | |
for(j = 0;j < n-i;j++ ) { | |
if (array[j] > array[j+1]) { | |
tmp = array[j]; | |
array[j] = array[j+1]; | |
array[j+1] = tmp; | |
} | |
} | |
} | |
} | |
void insertSort(vector<int> &nums) { | |
int size = nums.size(); | |
for(int i = 1; i < size; i++) { | |
int j = i - 1; | |
int value = nums[i]; | |
while(j >= 0 && nums[j] > value) { | |
nums[j+1] = nums[j]; | |
j--; | |
} | |
nums[j+1] = value; | |
} | |
} | |
void quickSort(int array[], int low, int high) { | |
if(low < high) { | |
int i = low, j = high,x = array[low]; | |
while (i < j) { | |
while (i < j && array[j] >= x ) { | |
j--; | |
} | |
if ( i < j) { | |
array[i++] = array[j]; | |
} | |
while (i < j && array[i] < x) { | |
i++; | |
} | |
if (i < j) { | |
array[j--] = array[i]; | |
} | |
} | |
array[i] = x; | |
quickSort(array,low,i-1); | |
quickSort(array,i+1,high); | |
} | |
} | |
void quickSort2(int array[],int low,int high) { | |
int i,j,t,temp; | |
if (low > high) { | |
return; | |
} | |
temp = array[low]; | |
i = low;j = high; | |
while (i < j) { | |
while (array[j] >= temp && i < j) { | |
j--; | |
} | |
while (array[i] < temp && i < j) { | |
i++; | |
} | |
if(i < j) { | |
t = array[i]; | |
array[i] = array[j]; | |
array[j] = t; | |
} | |
} | |
array[low] = array[i]; | |
array[i] = temp; | |
quickSort2(array,low,i-1); | |
quickSort2(array,i+1,high); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment