[数据结构] 快速排序+折半搜索


数据结构的练习与巩固
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

 1 int Quick_Search(int List[], int Length, int Number)
 2 {
 3      int Low, High, Middle;
 4      Low = 0;
 5      High = Length-1;
 6      
 7      while (High >= Low)
 8      {
 9          Middle = (Low + High) / 2;
10          if (List[Middle] == Number)
11              return Middle;
12          if (List[Middle] < Number)
13              Low = Middle + 1;
14          if (List[Middle] > Number)
15              High = Middle - 1;
16      }
17      return -1;
18 }
19  
20 //快速排序
21 void Quick_Sort(int List[], int Low, int High)
22 {
23      int i, j;
24      i = Low;
25      j = High;
26      int Key = List[Low];
27  
28      if(Low < High)
29      {
30          while (j > i)
31          {
32              while (List[j] >= Key && j > i)
33                  j--;
34              if (j > i)
35                  List[i++] = List[j];
36  
37              while (List[i] < Key && j > i)
38                  i++;
39              if (j > i)
40                  List[j--] = List[i];
41          }
42          List[i] = Key;
43  
44          Quick_Sort(List, Low, i - 1);
45          Quick_Sort(List, i + 1, High);
46      } 
47 }

 

 
posted @ 2020-07-03 19:52  蔚蓝の天空  阅读(175)  评论(0编辑  收藏  举报