- 论坛徽章:
- 1
|
- int partition(int *array, int low, int high)
- {
- int prov = array[low];
- while(low < high)
- {
- while(low < high && array[high] >= prov) high--;
- array[low] = array[high];
- while(low < high && array[low] <= prov) low++;
- array[high] = array[low];
- }
- array[low] = prov;
- return low;
- }
- void qsort(int *array, int low, int high)
- {
- if (low < high)
- {
- int p = partition(array, low, high);
- qsort(array, low, p - 1);
- qsort(array, p + 1, high);
- }
- }
- int num;
- cout << "inpput size:";
- cin >> num;
- int times = 0;
- cout << "input times:";
- cin>>times;
- int *a = new int[num];
- int *b = new int[num];
- clock_t m_start;
- unsigned long t1, t2;
- srand((unsigned int)time(NULL));
-
- int i = 0;
- while(++i < times)
- {
- for (int i = 0; i < num; ++i)
- {
- int temp = rand()%10000000;
- b[i] = a[i] = temp;
- }
- m_start = clock();
- //MergeSort(a, num);
- qsort(a, 0, num - 1); //
- // qsort(a, num, sizeof(int), compare);//
- t1 = clock() - m_start;
- cout << "t1 = " << (double)(t1)/CLOCKS_PER_SEC << "seconds" <<endl;
- m_start = clock();
- //HeapSort(b, num);
- qsort(b, num, sizeof(int), compare);
- t2 = clock() - m_start;
- cout << "t2 = " << (double)(t2)/CLOCKS_PER_SEC << "seconds" <<endl;
- //show_array(a, num);
- //show_array(b, num);
- if (!isthesame(a, b, num)) break;
- else cout << "two arrays is the same.diff = " << (long)(t2 - t1) << endl;
- Sleep(300);
- }
- delete [] a;
- delete [] b
复制代码 |
|