- 冒泡排序:
void bubble_sort(int* nums,int numsSize) {
int tmp;
for (int i = 0; i < numsSize; i++) {
for (int j = 0; j < numsSize-1; j++) {
if (nums[j] > nums[j+1]) {//升序排列,交换两数
tmp = nums[j];
nums[j] = nums[j+1];
nums[j+1] = tmp;
}
}
}
}
2.快速排序:
void quick_sort(int* nums,int left,int right){
if(left<right){
int i = left, j = right;
int key = nums[left];
while(i<j){
while(i<j&&nums[j]>=key){
j--;
}
if(i<j){
nums[i++] = nums[j];
}
while(i<j&&nums[i]<key){
i++;
}
if(i<j){
nums[j--] = nums[i];
}
}
nums[i] = key;
quick_sort(nums, left, i - 1);
quick_sort(nums, i + 1, right);
}
}