Last active
December 13, 2017 07:27
-
-
Save varshneydevansh/2c69bc559c2354f4864cc95d974abbd8 to your computer and use it in GitHub Desktop.
Quick sort using file handling in C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*---------------------------------------------------------------------------------. | |
Implementation of QUICK SORT Algorithms. | | |
Implemented By - Devansh Varshney CSE3_E E_12 | | |
Codechef ID - devansh777 | | |
HackerRank/ HackerEarth ID - varshenydevansh | | |
Stack Overflow - varshneydevansh | | |
Git-Hub ID - varshneydevansh | | |
-----------------------------------------------------------------------------------' | |
*/ | |
#include <iostream> | |
#include <fstream> | |
using namespace std; | |
int partition(int A[], int lo, int hi) | |
{ | |
int pivot, temp,i; | |
pivot = A[hi]; | |
i = lo - 1; | |
for (int j = lo; j < hi ; j++) | |
{ | |
if (A[j] < pivot) | |
{ | |
i = i + 1; | |
temp = A[i]; | |
A[i] = A[j]; | |
A[j] = temp; | |
} | |
} | |
if (A[hi] < A[i + 1]) | |
{ | |
temp = A[i+1]; | |
A[i + 1] = A[hi]; | |
A[hi] = temp; | |
} | |
return i + 1; | |
} | |
void quickSort(int arr[], int lo, int hi) | |
{ | |
int p; | |
if (lo < hi) | |
{ | |
p = partition(arr, lo, hi); | |
quickSort(arr, lo, p - 1); | |
quickSort(arr, p + 1, hi); | |
} | |
} | |
int main(void) | |
{ | |
int n; | |
ofstream my_out("QuickSort.out"); //Generates the output file | |
cout<<"Enter the no. of elements : \n"; | |
my_out << "Enter the no. of elements : " << endl; | |
cin >> n; | |
my_out << n << endl; | |
int arr[n]; | |
cout << "Enter the Elements : "; | |
my_out << "Enter the Elements : " << endl; | |
for(int arr_i = 0; arr_i < n; arr_i++ ) | |
{ | |
cin >> arr[arr_i]; | |
my_out << arr[arr_i] << endl; | |
} | |
quickSort(arr, 0, n-1); | |
cout << "Sorted array is : \n"; | |
my_out << "Sorted array is : " << endl; | |
for(int arr_i = 0; arr_i < n; arr_i++ ) | |
{ | |
cout << arr[arr_i]<< "\t" << endl; | |
my_out << arr[arr_i] << "\t" << endl; | |
} | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment