How can I make this c++ program faster? - c++

I am trying to sort strings using merge sort and standard string comparison. The problem is, this program is very slow as is. I am exceeding runtime expectations when I am attempting to check my solution. Is there a way I can use some other data structure to do the same thing but be accessed faster? Here is the code. Thank you for your help.
Edit: I understand that using sort() rather than my own merge sort algorithm will be faster, but my requirement states to use merge sort. The problem is exceeding runtime requirements on SPOJ.
#include <iostream>
using namespace std;
void MergeSortA(int low , int high);
void MergeA(int low ,int mid ,int high);
string stringData[50000];
int main()
{
int testCases;
int linesInTestCase;
scanf("%d", &testCases);
while(testCases--)
{
scanf("%d", &linesInTestCase);
for(int i = 0; i < linesInTestCase; ++i)
cin >> stringData[i];
// Sort the data
MergeSortA(0, linesInTestCase - 1);
// Print array of strings one by one
for(int i = 0; i < linesInTestCase; ++i) {
cout << stringData[i] << endl;
}
}
return 0;
}
void MergeSortA(int low , int high)
{
int mid = 0;
if(low < high)
{
mid = ((low+high)/2);
MergeSortA(low , mid);
MergeSortA(mid+1,high);
MergeA(low,mid,high);
}
}
void MergeA(int low ,int mid , int high)
{
int i = low, j = mid+1 , k = low;
string Temp[50000];
while(i <= mid && j <= high) {
if( stringData[i] < stringData[j] ) {
Temp[k].assign(stringData[i]);
i++;
}
else {
Temp[k].assign(stringData[j]);
j++;
}
k++;
}
if(i > mid ) {
for(int h = j ;h <= high ; h++ ) {
Temp[k].assign(stringData[h]);
k++;
}
}
else
for(int h = i; h<= mid ; h++ ) {
Temp[k].assign(stringData[h]);
k++;
}
// Copy from low to high
for(int l = low; l <= high ; l++) {
stringData[l].assign(Temp[l]);
}
}

As you stated that this is SPOJ problem, I will try not alter your structure of your program, However, there is real bottleneck here:
void MergeA(int low ,int mid , int high)
{
int i = low, j = mid+1 , k = low;
string Temp[50000]; //<--- Allocation happens every time this function is called
Wnenever MergeA is called, 50000 strings are constructed & destructed every time, encumbering the algorithm. Just add static there:
void MergeA(int low ,int mid , int high)
{
int i = low, j = mid+1 , k = low;
static string Temp[50000]; //Now it is constructed & destructed only once in the program

Related

How to sort data in linked list by Quicksort?

I want to sort the data in a linked list by Quicksort . Here is my code:
struct stu
{
int id;
char name[100];
int score;
stu* next;
}head;
int address(stu* StudentList)
{
//return the index of data
int index = 0;
stu* test = head;
for (index = 0; test != StudentList; temp++)
{
test = test->next;
}
return index;
}
stu* section(int low)
{
//Classification discussion on data
stu* StudentList1=head;
for (int i = 0; i < low; i++)
{
StudentList1 = StudentList1->next;
}
return StudentList1;
}
void swap(stu *Student1,stu *Student2)
{
int t_id = 0, t_score = 0;
char t_name[10] = "000";
//exchange id
t_id = Student1->id;
Student1->id = Student2->id;
Student2->id = t_id;
//exchange name
strcpy(t_name, Student1->name);
strcpy(Student1->name, Student2->name);
strcpy(Student2->name, t_name);
//exchange score
t_score = Student1->score;
Student1->score = Student2->score;
Student2->score = t_score;
}
void sort(int low, int high)
{
stu* StudentList1 = head, * StudentList2 = StudentList1->next;
if (low >= high)
return;
else
{
StudentList1=section(low);
StudentList2 = StudentList1->next;
stu* key = StudentList1;
int i;
for (int i = 0; i <= (high - low) && StudentList2 != NULL; i++)
{
if (StudentList2->score >= key->score)
{
StudentList1 = StudentList1->next;
swap(StudentList1, StudentList2);
}
StudentList2 = StudentList2->next;
}
swap(key, StudentList1);
int temp = address(StudentList1);
StudentList2 = head;
for (i = 0; StudentList2->next!=NULL;StudentList2=StudentList2->next)
{
if (StudentList2->next->score <= StudentList2->score)
{
i++;
}
}
high = temp - 1;
low = temp + 1;
if (i < num - 1)
{
sort(0, high);
sort(low, num - 1);
}
return;
}
}
The problem is the The program is in a infinite loop . I tried to modify it but it didn't help . So I have to turn into Stackoverflow . Thank you !
I just modified my code . But there are still some problems . When processing 7 data , the program can run well . However , when processing 8 data , the compiler told me stack overflow in "section" . Hope that you can point out the problems . Thank you sincerely !
One potential issue is this line:
sort(0, high);
The recursive calls should always be at least one node less than sort(lo, hi). There may be other issues.
Assuming this is a class assignment, I'm not sure why quicksort was chosen as a method for sorting a linked list, when merge sort is normally used. There is also the choice of "sorting" a linked list by rearranging the data in the nodes, versus sorting by reordering the nodes within a linked list. Since the question doesn't specify the assignment requirements, I can't be sure what the goal is here.
Using indexing for a linked list is inefficient. You could use pointer based quicksort such as:
void QuickSort(stu * lo, stu * hi);
Here is an a variation of Lomuto partition scheme that can be converted to sort a linked list, where lo, hi, i, j, k would be converted to pointers to node.
void QuickSort(int a[], int lo, int hi);
void QuickSort(int a[])
{
int lo = 0;
int hi = sizeof(a)/sizeof(a[0])-1;
if(lo >= hi)
return;
QuickSort(a, lo, hi);
}
void QuickSort(int a[], int lo, int hi)
{
int p = a[lo];
int i = lo;
int j = lo;
int k;
for(k = lo+1; k <= hi; k++){
if(a[k] < p){
j = i++;
std::swap(a[k], a[i]);
}
}
std::swap(a[i], a[lo]);
if(j != lo)
QuickSort(a, lo, j);
if(i != hi)
QuickSort(a, i+1, hi);
}
You could also consider a quicksort like method, that creates 3 lists with each level of recursion, sorting nodes instead of swapping data. The 3 lists would be nodes < pivot, nodes == pivot, nodes > pivot. Recursion would be used on the 2 lists with nodes != pivot, then the 3 lists would be concatenated.

I am trying to implement Merge sort algorithm from (Introduction to Algorithms (CLRS)) using a vector in c++ but the array is not sorting,

I am trying to implement Merge sort algorithm from (Introduction to Algorithms (CLRS)) using a vector in c++ but the vector is not sorting,
please help, where am i going wrong,
I am trying to use call by refference
The input is from a text file which contains integers in new line
#include <iostream>
#include <fstream>
#include <vector>
#include <limits>
using namespace std;
void merge(vector<long>& A, long high, long mid, long low)
{
long n1,n2;
n1 = mid -low +1;
n2 = high - mid;
vector<long> L; //auxilarry array
vector<long> R; //auxilarry array
for(int i = low; i <= mid;i++)
{
L.push_back(A[i]);
}
for(int i = mid+1; i <= high;i++)
{
R.push_back(A[i]);
}
L.push_back(numeric_limits<int>::max());
R.push_back(numeric_limits<int>::max());
int i = 0, j = 0;
for(int k = low; k <= high; k++)
{
if(L[i]<=R[j])
{
A[k] = L[i];
i++;
}
else
{
A[k] = R[j];
j++;
}
}
}
void mergeSort(vector<long>& A, long low, long high)
{
long mid;
if(low < high)
{
mid = (high+low)/2 ;
mergeSort(A,low,mid);
mergeSort(A, mid+1,high);
merge(A,low, mid,high);
}
}
int main()
{
ifstream fin("Array.txt");
vector<long> array;
for(long i;fin>>i;)
{
array.push_back(i);
}
cout<<array.size();
mergeSort(array,0,array.size());
for(long i=0;i<array.size();i++)
{
cout<<array[i]<<endl;
}
return 0;
}
your merge function is declared as :
void merge(vector<long>& A, long high, long mid, long low)
and u are calling merge inside mergesort by.
void merge(array, low, mid, high)
Ordering of arguments is wrong. See low and high variable ordering

Merge Sort: Heap Corruption on delete[]

Working on a class project in which i need to implement a Merge Sort to sort 500,000 items.
After many attempts I tried looking for source code online and found some here: http://www.sanfoundry.com/cpp-program-implement-merge-sort/
I had to alter the code to use a dynamic array (for size). When the program runs the merge function, I create a new, dynamic array using the number of elements (or high) that are being merged. Once the function is finished sorting them and merge them into the original array, i use delete[] on the new dynamic array. This is where I get my "Heap Corruption Detected" error.
Here is the code (wall of text):
//Heap Sort
#include <iostream>
#include <fstream>
#include <sstream>
#include <ctime>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
//Function Prototypes
void mergesort(int *a, int low, int high);
void merge(int *a, int low, int high, int mid);
int main()
{
//Start with element 1 of the array
int line_no = 0;
int num;
int array_size = 500000;
int* num_array = new int[array_size];
//Open file for input
fstream in_file("CSCI3380_final_project_dataset.txt", ios::in);
//Test for file opening
if (!in_file)
{
cout << "Cannot open words1.txt for reading" << endl;
exit(-1);
}
//Read file
while(true)
{
//Read one line at a time
in_file >> num;
//Test for eof
if (in_file.eof())
break;
num_array[line_no] = num;
//Increment array position
line_no++;
}
//Close the file
in_file.close();
//Start Time
clock_t time_a = clock();
//Run Sorting Algorithim
mergesort(num_array, 0, array_size-1);
//End Time
clock_t time_b = clock();
//Elapsed Time
if (time_a == ((clock_t)-1) || time_b == ((clock_t)-1))
{
cout << "Unable to calculate elapsed time" << endl;
}
else
{
int total_time_ticks = time_b - time_a;
cout << "Elapsed time: " << total_time_ticks << endl;
}
delete[] num_array;
return 0;
}
void mergesort(int *a, int low, int high)
{
int mid;
if (low < high)
{
mid=(low+high)/2;
mergesort(a,low,mid);
mergesort(a,mid+1,high);
merge(a,low,high,mid);
}
return;
}
void merge(int *a, int low, int high, int mid)
{
//--------------------------Create new array-------------------------------
int* sort_array = new int[high];
//--------------------------New Array Created-----------------------------
int i, j, k;
i = low;
k = low;
j = mid + 1;
while (i <= mid && j <= high)
{
if (a[i] < a[j])
{
sort_array[k] = a[i];
k++;
i++;
}
else
{
sort_array[k] = a[j];
k++;
j++;
}
}
while (i <= mid)
{
sort_array[k] = a[i];
k++;
i++;
}
while (j <= high)
{
sort_array[k] = a[j];
k++;
j++;
}
for (i = low; i < k; i++)
{
a[i] = sort_array[i];
}
//---------------------------Delete the New Array--------------------
delete[] sort_array;
//--------------------------Oh No! Heap Corruption!------------------
}
I'll spare you the "you should be using vectors", "you should be using smart pointers", etc. You should be, and I'll leave it at that. Regarding your actual problem....
You're writing one-past the allocated space of your array. The allocated size is high:
int* sort_array = new int[high];
meaning you can only dereference from 0..(high-1). Yet this:
while (j <= high)
{
sort_array[k] = a[j];
k++;
j++;
}
is one location that is guaranteed to write to sort_array[high], and therefore invoke undefined behavior.
A Different Approach
Mergesort is about div-2 partitioning. You know this. What you may not have considered is that C and C++ both perform pointer-arithmetic beautifully and as such you only need two parameters for mergesort(): a base address and a length. the rest can be taken care of for you with pointer math:
Consider this:
void mergesort(int *a, int len)
{
if (len < 2)
return;
int mid = len/2;
mergesort(a, mid);
mergesort(a + mid, len-mid);
merge(a, mid, len);
}
And a merge implementation that looks like this:
void merge(int *a, int mid, int len)
{
int *sort_array = new int[ len ];
int i=0, j=mid, k=0;
while (i < mid && j < len)
{
if (a[i] < a[j])
sort_array[k++] = a[i++];
else
sort_array[k++] = a[j++];
}
while (i < mid)
sort_array[k++] = a[i++];
while (j < len)
sort_array[k++] = a[j++];
for (i=0;i<len;++i)
a[i] = sort_array[i];
delete[] sort_array;
}
Invoked from main() like the following. Note: I've removed the file i/o in place of a random generation just to make it easier to test:
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <cstdio>
using namespace std;
//Function Prototypes
void mergesort(int *a, int len);
void merge(int *a, int mid, int len);
int main()
{
std::srand((unsigned int)std::time(nullptr));
// Start with element 1 of the array
int array_size = 500000;
int* num_array = new int[array_size];
std::generate_n(num_array, array_size, std::rand);
// Start Time
clock_t time_a = clock();
// Run Sorting Algorithim
mergesort(num_array, array_size);
// End Time
clock_t time_b = clock();
//Elapsed Time
if (time_a == ((clock_t)-1) || time_b == ((clock_t)-1))
{
cout << "Unable to calculate elapsed time" << endl;
}
else
{
int total_time_ticks = time_b - time_a;
cout << "Elapsed time: " << total_time_ticks << endl;
}
delete[] num_array;
return 0;
}
This resulted is an elapsed time of:
Elapsed time: 247287
More Efficient
By now you've seen that you will need at most N-space in addition to you sequence. The top-most merge should e evidence enough of that. What you may not consider is that in-reality that is exactly the space you need, and you can allocate it up-front and use it throughout the algorithm if you desire. You can keep the current entrapping for mergesort(), but we'll be wrapping it up with a front-loader that allocates all the space we'll ever need once:
// merges the two sequences a[0...mid-1] and a[mid...len-1]
// using tmp[] as the temporary storage space
static void merge_s(int *a, int *tmp, int mid, int len)
{
int i=0, j=mid, k=0;
while (i < mid && j < len)
{
if (a[i] < a[j])
tmp[k++] = a[i++];
else
tmp[k++] = a[j++];
}
while (i < mid)
tmp[k++] = a[i++];
while (j < len)
tmp[k++] = a[j++];
for (i=0;i<len;++i)
a[i] = tmp[i];
}
static void mergesort_s(int *a, int *tmp, int len)
{
if (len < 2)
return;
int mid = len/2;
mergesort_s(a, tmp, mid);
mergesort_s(a + mid, tmp+mid, len-mid);
merge_s(a, tmp, mid, len);
}
void mergesort(int *a, int len)
{
if (len < 2)
return;
int *tmp = new int[len];
mergesort_s(a,tmp,len);
delete [] tmp;
}
This resulted in an elapsed time of:
Elapsed time: 164704
Considerably better than we had before. Best of luck.
The copy step shown in WhozCraig's code example can be avoided using a pair of functions to control the direction of the merge (note - a bottom up merge would still be faster).
Note - I wouldn't recommend using either WhozCraig's or my code example, since these methods were probably not covered in your class, and it's supposed to be code written based on what you were taught in your class. I don't know if bottom up merge sort was covered in your class, so I didn't post an example of it.
mergesort_s(int *a, int *tmp, int len)
{
// ...
mergesort_atoa(a, tmp, 0, len);
// ...
}
mergesort_atoa(int *a, int *tmp, int low, int end)
{
if((end - low) < 2){
return;
}
int mid = (low + end) / 2;
mergesort_atot(a, tmp, low, mid);
mergesort_atot(a, tmp, mid, end);
merge_s(tmp, a, low, mid, end);
}
mergesort_atot(int *a, int *tmp, int low, int end)
{
if((end - low) < 2){
tmp[0] = a[0];
return;
}
int mid = (low + end) / 2;
mergesort_atoa(a, tmp, low, mid);
mergesort_atoa(a, tmp, mid, end);
merge_s(a, tmp, low, mid, end);
}
void merge_s(int *src, int *dst, int low, int mid, int end)
{
int i = low; // src[] left index
int j = mid; // src[] right index
int k = low; // dst[] index
while(1){ // merge data
if(src[i] <= src[j]){ // if src[i] <= src[j]
dst[k++] = src[i++]; // copy src[i]
if(i < mid) // if not end of left run
continue; // continue (back to while)
while(j < end) // else copy rest of right run
dst[k++] = src[j++];
return; // and return
} else { // else src[i] > src[j]
dst[k++] = src[j++]; // copy src[j]
if(j < end) // if not end of right run
continue; // continue (back to while)
while(i < mid) // else copy rest of left run
dst[k++] = src[i++];
return; // and return
}
}
}

C++ Getting StackOverflow error in quicksort function

I am getting stackoverflow error when I am trying to sort using quicksort an array of large size, and this array is in descending order. I want to sort it in ascending order using the code below:
int partition_lastElementPivot(int * arr, int lo, int hi)
{
int x = arr[hi];
int i = lo - 1;
for (int j = lo; j < hi; j++)
{
if (arr[j] <= x)
{
i++;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
int temp = arr[hi];
arr[hi] = arr[i + 1];
arr[i + 1] = temp;
return i + 1;
}
void quicksortLastElementPivot(int*arr, int lo, int hi)
{
if (lo<hi)
{
int mid = partition_lastElementPivot(arr, lo, hi);
quicksortLastElementPivot(arr, lo, mid - 1);
quicksortLastElementPivot(arr, mid + 1, hi);
}
}
This code works fine when I randomly generate an array of any size, suppose of size 5000. But when I generate an array of size 5000 sorted in descending order and then try to sort using this code, I get a stackoverflow error. Does C++ limits the memory useable by stack and why is this happening.
int arr[5000];
int count = 5001;
for(int i=0; i<5000; i++)
{
arr[i] = count;
count--;
}
quicksortLastElementPivot(arr, 0, 4999)
Thanks
Quicksort has a truly dreadful worst-case performance, as you have discovered here. It is calling itself at a stack depth of 5000. This Wikipedia article has a good discussion on the subject. In particular, it mentions tail recursion as a solution to your stack overflow problem.
Briefly, this means that instead of the last call to quicksortLastElementPivot, followed immediately by a return, you just loop back to the start of the function. This has the same effect, but the tail recursion doesn't increase the stack size. For this to work, you have to make sure that the smaller of the two partitions is sorted first, using traditional recursion, and the larger partition is sorted by tail recursion. Something like this (not tested!):
void quicksortLastElementPivot(int*arr, int lo, int hi)
{
TailRecurse:
if (lo<hi)
{
int mid = partition_lastElementPivot(arr, lo, hi);
if (mid < (lo + hi) / 2)
{ // First partition is smaller
quicksortLastElementPivot(arr, lo, mid - 1); // Sort first partition
lo = mid + 1; goto TailRecurse; // Sort second partition
}
else
{ // Second partition is smaller
quicksortLastElementPivot(arr, mid + 1, hi); // Sort second partition
hi = mid - 1; goto TailRecurse; // Sort first partition
}
}
}
C++ standard does not define the stack-size of an executable program.
This limit is typically defined in the make file or the linker-command file of your project.
Depending on your IDE, you might also find it within your project settings (under linker-configuration).
The answer given by TonyK is doing quite a good job in explaining the stack usage of quick-sort under the worst-case scenario (which is exactly the case in your code, where arr is sorted in reversed order).
#include <iostream>
using namespace std;
void QuickSort(int *arr, int left, int right)
{
int i = left;
int j = right;
int pivot = arr[rand() % (right - left) + left];
while (i < j)
{
while (arr[i] < pivot)
{
i++;
}
while (arr[j] > pivot)
{
j--;
}
if (i <= j)
{
swap(arr[i], arr[j]);
i++;
j--;
}
}
if (left < j)
{
QuickSort(arr, left, j);
}
if (i < right)
{
QuickSort(arr, i, right);
}
}
int main()
{
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
int n;
cin >> n;
int *arr = new int[n];
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
QuickSort(arr, 0, n - 1);
for (int i = 0; i < n; i++)
{
cout << arr[i] << " ";
}
delete arr;
}

C++ quick sort algorithm

I'm not looking to copy a qsort algorithm. I'm practicing writing qsort and this is what I've come up with and I'm interested in what part of my code is wrong. Please don't tell me that this is homework cause I could just use the code in the link below.
Reference: http://xoax.net/comp/sci/algorithms/Lesson4.php
When this runs I get this in the console:
Program loaded.
run
[Switching to process 10738]
Running…
Current language: auto; currently c++
Program received signal: “EXC_ARITHMETIC”.
void myQSort(int min, int max, int* myArray)
{
// Initially find a random pivot
int pivotIndex = rand() % max;
int pivot = myArray[pivotIndex];
int i = 0 , j = max-1;
// Pointer to begining of array and one to the end
int* begin = myArray;
int* end = &myArray[max-1];
// While begin < end
while( begin < end )
{
// Find the lowest bound number to swap
while( *begin < pivot )
{
begin++;
}
while( *end > pivot )
{
// Find the highest bound number to swap
end--;
}
// Do the swap
swap(begin,end);
}
// Partition left
myQSort(0, pivotIndex-1, myArray);
// Partiion right
myQSort(pivotIndex+1,max, myArray);
}
EDIT--
Code for Swap:
void swap(int* num, int* num2)
{
int temp = *num;
*num = *num2;
*num2 = temp;
}
// sort interval [begin, end)
void myQSort(int* begin, int* end)
{
if(end - begin < 2)
return;
int* l = begin;
int* r = end - 1;
// Initially find a random pivot
int* pivot = l + rand() % (r - l + 1);
while(l != r)
{
// Find the lowest bound number to swap
while(*l < *pivot) ++l;
while(*r >= *pivot && l < r) --r;
// Do the swap
if(pivot == l) { pivot = r; }
std::swap(*l, *r);
}
// Here l == r and numbers in the interval [begin, r) are lower and in the interval [l, end) are greater or equal than the pivot
// Move pivot to the position
std::swap(*pivot, *l);
// Sort left
myQSort(begin, l);
// Sort right
myQSort(l + 1, end);
}
You're not using the min parameter in your code, anywhere. You need to set begin and your pivot value using that.
I tried working out the codes above. But, they don't compile.
#Mihran: Your solution is correct algorithmically but the following line generates an error:
myQSort(min, begin - myArray, myArray);
This is because begin is of type int* and myArray is of type long, following which the compiler shows this error message:
implicit conversion loses integer precision
Here's a working solution in C++:
#include <iostream>
using namespace std;
void mySwap(int& num1, int& num2){
int temp = num1;
num1 = num2;
num2 = temp;
}
void myQsort(int myArray[], int min, int max){
int pivot = myArray[(min + max) / 2];
int left = min, right = max;
while (left < right) {
while (myArray[left] < pivot) {
left++;
}
while (myArray[right] > pivot) {
right--;
}
if (left <= right) {
mySwap(myArray[left], myArray[right]);
left++;
right--;
}
}
if (min < right) {
myQsort(myArray, min, right);
}
if (left < max) {
myQsort(myArray, left, max);
}
}
int main()
{
int myArray[] = {1, 12, -5, 260, 7, 14, 3, 7, 2};
int min = 0;
int max = sizeof(myArray) / sizeof(int);
myQsort(myArray, min, max-1);
for (int i = 0; i < max; i++) {
cout<<myArray[i]<<" ";
}
return 0;
}
Here's a clear C++ implementation, for reference:
#include <iostream>
#include <vector>
using namespace std;
int partition(std::vector<int>& arr, int low, int high) {
// set wall index
int wall_index = low;
int curr_index = low;
int pivot_elem = arr[high]; // taking last element as pivot_element
// loop through the entire received arr
for (int i = curr_index; i < high; ++i) {
// if element is less than or equal to pivot_elem
// swap the element with element on the right of the wall
// i.e swap arr[i] with arr[wall_index]
if (arr[i] <= pivot_elem) {
// swap
int temp = arr[wall_index];
arr[wall_index] = arr[i];
arr[i] = temp;
// move the wall one index to the right
wall_index++;
curr_index++;
} else {
// if the element is greater than the pivot_element
// then keep the wall at the same point and do nothing
curr_index++;
}
}
// need to swap the pivot_elem i.e arr[high] with the element right of the wall
int temp = arr[wall_index];
arr[wall_index] = arr[high];
arr[high] = temp;
return wall_index;
}
void quick_sort(std::vector<int>& arr, int low, int high) {
if (low < high) { // element with single arr always have low >= high
int split = partition(arr, low, high);
quick_sort(arr, low, split-1);
quick_sort(arr, split, high);
}
}
int main() {
std::vector<int> data = {6,13,8,4,2,7,16,3,8};
int N = data.size();
quick_sort(data, 0, N-1);
for (int i : data) {
cout << i << " ";
}
return 0;
}
I don't see a clean implementation of Quicksort on SO, so here is my easy to understand implementation
PLEASE DONT USE IN PRODUCTION CODE
This is only for your understanding
// Swap position a with b in an array of integer numbers
void swap(int *numbers, int a, int b){
int temp = numbers[a];
numbers[a] = numbers[b];
numbers[b] = temp;
}
static int partition(int *data, int low, int high) {
int left = low, right = high, pivot = data[low];
while (left < right) {
// Everthing on the left of pivot is lower than the pivot
while ((left <= right) && data[left] <= pivot) // <= is because left is the pivot initially
left++;
// Everything on the right of the pivot is greater than the pivot
while((left <= right) && data[right] > pivot)
right--;
if (left < right)
swap(data, left, right);
}
// Put the pivot in the 'rigthful' place
swap(data, low, right);
return right;
}
// Quicksort
static void quick_sort(int *numbers, int low, int high)
{
if (high > low) {
int p_index = partition(numbers, low, high);
quick_sort(numbers, low , p_index - 1);
quick_sort(numbers, p_index + 1, high);
}
}