Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions C pp/sort/quickSort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include <bits/stdc++.h>
using namespace std;

class quickSort
{
public:

void QuickSort(int arr[], int low, int high)
{
if (low < high) {

int pivot = partition(arr, low, high) ;
QuickSort(arr, low, pivot - 1) ;
QuickSort(arr, pivot + 1, high) ;
}
}


int partition (int arr[], int low, int high)
{
int pivot = arr[low] ;
int i = low ;
int j = high ;

while (i < j) {

while (arr[i] <= pivot && i <= high - 1) {
i++ ;
}

while (arr[j] > pivot && j >= low) {
j-- ;
}

if (i < j)
swap(arr[i], arr[j]) ;
}

swap(arr[j], arr[low]) ;

return j ;
}
};

void printArray(int arr[], int n)
{
for (int i = 0; i < n; i++) {
cout << arr[i] << " " ;
}
cout<<endl;
}

int main()
{

int arr[] = {4, 6, 2, 5, 7, 8, 1, 3} ;
int n = sizeof(arr) / sizeof(arr[0]) ;
Solution obj;
cout<<"Before Quick Sort: "<<endl;
printArray(arr,n);
obj.QuickSort(arr, 0, n - 1);
cout << "After Quick Sort: "<<endl;
printArray(arr, n);
return 0;
}
38 changes: 38 additions & 0 deletions C pp/sort/quickSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
public class quickSort {
public static void main(String[] args){
int[] baseArray = { 20, 23, 54, 9, 18, 78 };

for(int i = 0; i< baseArray.length ; i++){
System.out.println(baseArray[i]);
}
}

public static void QuickSort(int[] input, int begin, int end) {
if (end - begin <2){
return;
}

int index = partition(input,begin,end);
QuickSort(input,begin,index);
QuickSort(input,index+1,end);
}

public static int partition(int[] input, int begin, int end){
int pivot =input[begin];
int i= begin;
int j= end;

while(i<j){
while (i<j && input[--j] >= pivot);
if (i<j){
input[i]=input[j];
}
while ( i<j && input[++i] <= pivot);
if( i<j ){
input[j]=input[i];
}
}
input[j] = pivot;
return j;
}
}