1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
|
#include <iostream>
const short SIZE = 4390;
void swapInnanet(int* a, int* b);
int partitionInnanet(int arr[], int low, int high);
void quickSortInnanet(int arr[], int low, int high);
////////////////////////////////////////////////////////////
void swap(int a, int b);
int partition(int arr[], int low, int high);
void quickSort(int arr[], int low, int high);
//////////////////////////////////////////////////////////////
int Hpartition(int arr[], int low, int high);
void HquickSort(int arr[], int low, int high);
int main()
{
int array[SIZE];
for (int i = 0; i < SIZE; i++)
array[i] = SIZE - i;
for (int i = 0; i < SIZE; i++)
std::cout << array[i] << std::endl;
//quickSort(array, 0, SIZE);
//quickSort(array, 0, SIZE - 1);
//HquickSort(array, 0, SIZE - 1);
HquickSort(array, 0, SIZE);
for (int i = 0; i < SIZE; i++)
std::cout << array[i] << std::endl;
std::cin.get();
std::cin.get();
return 0;
}
// A utility function to swap two elements
void swapInnanet(int* a, int* b)
{
int t = *a;
*a = *b;
*b = t;
}
/* This function takes last element as pivot, places
the pivot element at its correct position in sorted
array, and places all smaller (smaller than pivot)
to left of pivot and all greater elements to right
of pivot */
int partitionInnanet(int arr[], int low, int high)
{
int pivot = arr[high]; // pivot
int i = (low - 1); // Index of smaller element
for (int j = low; j <= high - 1; j++)
{
// If current element is smaller than or
// equal to pivot
if (arr[j] <= pivot)
{
i++; // increment index of smaller element
swapInnanet(&arr[i], &arr[j]);
}
}
swapInnanet(&arr[i + 1], &arr[high]);
return (i + 1);
}
/* The main function that implements QuickSort
arr[] --> Array to be sorted,
low --> Starting index,
high --> Ending index */
void quickSortInnanet(int arr[], int low, int high)
{
if (low < high)
{
/* pi is partitioning index, arr[p] is now
at right place */
int pi = partitionInnanet(arr, low, high);
// Separately sort elements before
// partition and after partition
quickSortInnanet(arr, low, pi - 1);
quickSortInnanet(arr, pi + 1, high);
}
}
void swap(int a, int b)
{
int t = a;
a = b;
b = t;
}
void quickSort(int arr[], int low, int high)
{
if (low < high)
{
int q = partition(arr, low, high);
quickSort(arr, low, high - 1);
quickSort(arr, q + 1, high);
}
}
int partition(int arr[], int low, int high)
{
int pivot = arr[high];
int i = low - 1;
int j = low;
for (j; j < high; j++)
{
if (arr[j] <= pivot)
{
i++;
swap(arr[i], arr[j]);
}
}
i++;
swap(arr[i], arr[j]);
return i;
}
void HquickSort(int arr[], int low, int high)
{
if (low < high)
{
int q = Hpartition(arr, low, high);
HquickSort(arr, low, high);
HquickSort(arr, q + 1, high);
}
}
int Hpartition(int arr[], int low, int high)
{
int pivot = arr[low];
int i = low - 1;
int j = high + 1;
while (low)
{
do
{
i++;
} while (arr[i] < pivot);
do
{
j--;
} while (arr[j] > pivot);
if (i <= j)
return j;
swap(arr[i], arr[j]);
}
}
|