Skip to content

Instantly share code, notes, and snippets.

@lixianyang
Created July 16, 2017 06:04
Show Gist options
  • Select an option

  • Save lixianyang/0f7dbe888234798f32cd64d756f88f88 to your computer and use it in GitHub Desktop.

Select an option

Save lixianyang/0f7dbe888234798f32cd64d756f88f88 to your computer and use it in GitHub Desktop.
快速排序,c语言实现(基础版)
# include <stdio.h>
void swap(int v[], int i, int j){
int temp = v[i];
v[i] = v[j];
v[j] = temp;
}
void qsort(int v[], int left, int right){
int i, last;
if(left >= right)
return;
last = left;
for(i = left+1; i <= right; i++){
if(v[i] <= v[left])
swap(v, ++last, i);
}
swap(v, last, left);
qsort(v, left, last-1);
qsort(v, last+1, right);
}
int main(){
int v[] = {4, 2, 3, 1, 8, 6, 3, 7, 5, 10, 21, 19};
int length = 12;
qsort(v, 0, length-1);
for(int i=0; i<length; i++){
printf("%d%s", v[i], i == length-1 ? "\n": ", ");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment