Skip to content

Commit

Permalink
Merge pull request #33 from rattle99/master
Browse files Browse the repository at this point in the history
add quicksort
  • Loading branch information
i-vishi authored Oct 2, 2018
2 parents d81223d + 23472cf commit 7e81ecd
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 13 deletions.
64 changes: 64 additions & 0 deletions Sorting/quickSort/quick.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*Program for quick sort implementation in C++. */

#include <iostream>
#include <vector>

int partition(std::vector<int> &A, int start, int end)
{
int temp;
int pivot = A[end];
int partitionIndex = start; // set partition index as start initially
for(int i=start; i<end; i++)
{
if(A[i] <= pivot)
{
temp=A[i];
A[i]=A[partitionIndex];
A[partitionIndex]=temp;
partitionIndex++;
}
}
temp=A[partitionIndex]; // Swap pivot element
A[partitionIndex]=A[end]; // eith element at
A[end]=temp; // partition index

return partitionIndex;
}

std::vector<int> quick_sort(std::vector<int> &A, int start, int end)
{
/* std::cout<<"Current list : \n";
for(int i=0;i<A.size();i++)
{
std::cout<<A[i]<<" ";
}
std::cout<<"\nSorted list : \n";
*/

if(start<end)
{
int partitionIndex = partition(A,start,end);
quick_sort(A,start,partitionIndex-1);
quick_sort(A,partitionIndex+1, end);
}

/* for(int i=0;i<A.size();i++)
{
std::cout<<A[i]<<" ";
}
*/ return A;
}

int main()
{
std::cout<<"\nTest\n";
std::vector<int> arr={10,2,23,-4,235,56,2,6,5,5,5,23,-4,346,-56,81,43,654,435,-54};
quick_sort(arr, 0, arr.size()-1);
std::cout<<"\n\n\n";
for(int i=0;i<arr.size();i++)
{
std::cout<<arr[i]<<" ";
}
return 0;
}

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <iostream>
#include <stdlib.h>
#include <conio.h>


using namespace std;
void stck();
Expand Down Expand Up @@ -47,7 +47,7 @@ void stck_ins_arr()
st[top] = data;
}
cout << "\nElement inserted succesfully\n";
getch();
getchar();
stck();
}

Expand All @@ -63,7 +63,7 @@ void stck_del_arr()
top--;
cout << "\nElement deleted succesfully\n";
}
getch();
getchar();
stck();
}
void stck_print()
Expand All @@ -88,7 +88,7 @@ void stck_print()
<< "\n";
}
}
getch();
getchar();
stck();
}
void stck()
Expand Down Expand Up @@ -136,7 +136,7 @@ void que_ins_arr()

cout << "\nElement inserted succesfully\n";

getch();
getchar();
que();
}
void que_del_arr()
Expand All @@ -153,7 +153,7 @@ void que_del_arr()
cout << "\nElement deleted succesfully\n";
}

getch();
getchar();
que();
}
void que_print()
Expand All @@ -166,7 +166,7 @@ void que_print()
cout << q[i] << " | ";
}

getch();
getchar();
que();
}
void que()
Expand Down Expand Up @@ -214,7 +214,7 @@ void stck_ins_list()
head = temp;
}
cout << "\nElement insereted successfully\n";
getch();
getchar();
stck_list();
}
void stck_del_list()
Expand All @@ -232,7 +232,7 @@ void stck_del_list()
free(temp);
}
cout << "\nElement deleted succesfully!!\n";
getch();
getchar();
stck_list();
}
void stck_print_list()
Expand All @@ -258,7 +258,7 @@ void stck_print_list()
temp = temp->next;
}
}
getch();
getchar();
stck_list();
}

Expand Down Expand Up @@ -307,7 +307,7 @@ void que_ins_list()
head = temp;
}
cout << "\nElement inserted succesfully\n";
getch();
getchar();
que_list();
}
void que_del_list()
Expand Down Expand Up @@ -339,7 +339,7 @@ void que_del_list()
temp1->next = NULL;
}
cout << "\nElement deleted succesfully!!\n";
getch();
getchar();
que_list();
}
void que_print_list()
Expand All @@ -362,7 +362,7 @@ void que_print_list()
temp = temp->next;
}
}
getch();
getchar();
que_list();
}

Expand Down

0 comments on commit 7e81ecd

Please sign in to comment.