标签:
1 // ConsoleApplication1.cpp : 定义控制台应用程序的入口点。 2 // 3 4 #include "stdafx.h" 5 #include <stdio.h> 6 #include <iostream> 7 using namespace std; 8 void Print(int*num, int n) 9 { 10 int i; 11 for (i = 0; i < n; i++) 12 printf("%d ", num[i]); 13 puts("\n"); 14 return; 15 } 16 void Bubble_sort(int *num, int n){ 17 int i, j; 18 for (i = 0; i < n; i++) 19 { 20 for (j = 0; i+j < n-1; j++){ 21 if (num[j]>num[j+1]){ 22 int temp = num[j]; 23 num[j] = num[j+1]; 24 num[j+1] = temp; 25 } 26 27 } 28 Print(num, n); 29 } 30 } 31 void Quick_sort(int *num,int left,int right){ 32 if (left < right){ 33 int key = num[left]; //key is the base number 34 int low = left; 35 int high = right; 36 while (low != high){ 37 // search the number larger than it from right 38 while (num[high] >= key&&low < high) 39 high--; 40 //search the number smaller than it from left 41 while (num[low] <= key&&low < high) 42 low++; 43 //change the position of two array and in order to divide them into two parts according to base number,left is small,and right is big. 44 if (low < high) 45 { 46 int temp = num[low]; 47 num[low] = num[high]; 48 num[high] = temp; 49 } 50 } 51 //set the base number and recursive method to deal with two parts 52 num[left] = num[low]; 53 num[low] = key; 54 Quick_sort(num, left, low - 1); 55 Quick_sort(num, low + 1, right); 56 } 57 58 59 } 60 int _tmain(int argc, _TCHAR* argv[]) 61 { 62 int a; 63 int x[10] = { 31,65,82,76,13,27,10,3,2,1 }; 64 Quick_sort(x, 0, 9); 65 Print(x, 10); 66 cin >> a; 67 return 0; 68 69 }
标签:
原文地址:http://www.cnblogs.com/zhangqifire/p/5736276.html