大数快速排序

  大数快速排序

  这天做一道练习题,关于大数比较的。。。

  于是就有想法把大数比较融入排序算法中,结合快速排序非常简单。

代码如下:

 1 #include <string>
 2 #include <cstring>
 3 #include <iostream>
 4 using namespace std;
 5 
 6 void change(string &a, string &b){
 7     string t = a;
 8     a = b;
 9     b = t;
10 }
11 bool compare(string s[], int i, string k){    //s[i]>k,return true
12     int a = s[i].length(), b = k.length();
13 
14     if(k[0] == '-' && s[i][0] != '-'){
15         return true;
16     }else if(s[i][0] != '-' && k[0] != '-'){
17         if((a>b) || (a==b && s[i]>k)){
18             return true;
19         }
20     }else if(s[i][0] == '-' && k[0] == '-'){
21         if((a<b) || (a==b && s[i]<k)){
22             return true;
23         }
24     }
25     return false;
26 }
27 void bigQsort(string a[], int start, int end){
28     if(start >= end)
29         return ;
30     int i = start, j = end;
31     string k = a[start];
32     while(i != j){
33         while(i < j && compare(a, j, k))
34             j--;
35         change(a[i], a[j]);
36         while(i < j && !compare(a, i, k))
37             i++;
38         change(a[i], a[j]);
39     }
40     bigQsort(a, start, i);
41     bigQsort(a, i+1, end);
42 }
43 int main()
44 {
45     int n;
46     while(cin >> n){
47         string *s = new string[n];
48         for(int i = 0; i < n; i++)
49             cin >> s[i];
50         bigQsort(s,0,n-1);
51         cout << "排序结果:" << endl << s[0];
52         for(int i = 1; i < n; i++)
53             cout << " " << s[i];
54         cout << endl;
55     }
56     return 0;
57 }
原文地址:https://www.cnblogs.com/AardWolf/p/10072407.html