Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CPP- QuickSort #66

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
58 changes: 58 additions & 0 deletions Cpp-QuickSort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Quick sort algorithm in C++

#include <iostream>
using namespace std;

// Swapping position of elements
void swapPositions(int *a, int *b) {
int c = *a;
*a = *b;
*b = c;
}

// Printing elements of an array
void printArrayElements(int array[], int size) {
int i;
for (i = 0; i < size; i++){
cout << array[i] << " ";
}
cout << endl;
}

// Partitioning the array
int partitionArray(int array[], int low, int high,int n) {
int pivot = array[high]; //Pivot Element
int i = (low - 1);

for (int j = low; j < high; j++) {
if (array[j] <= pivot) {
i++;
swapPositions(&array[i], &array[j]);
}
}
printArrayElements(array, n);
cout << "______________________________________________\n";
swapPositions(&array[i + 1], &array[high]);
return (i + 1);
}

void quickSort(int array[], int low, int high,int n) {
if (low < high) {
int pi = partitionArray(array, low, high,n);

// Sorting elements on the left of pivot
quickSort(array, low, pi - 1,n);

// Sorting elements on the right of pivot
quickSort(array, pi + 1, high,n);
}
}


int main() {
int data[] = {9,10,11,4,6,3,7,1,5,0,15};
int n = sizeof(data) / sizeof(data[0]);
quickSort(data, 0, n - 1,n);
cout << "\nSorted array in ascending order using the Quick Sort Algorithm: \n";
printArrayElements(data, n);
}
62 changes: 31 additions & 31 deletions bubblesort.cpp
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
//CPP program for bubblesort of two array
#include<iostream>

using namespace std;

int main()
{
int a[50],n,i,j,temp;
cout<<"Enter the size of array: ";
cin>>n;
cout<<"Enter the array elements: ";

for(i=0;i<n;++i)
cin>>a[i];

for(i=1;i<n;++i)
{
for(j=0;j<(n-i);++j)
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}

cout<<"Array after bubble sort:";
for(i=0;i<n;++i)
cout<<" "<<a[i];

return 0;
//CPP program for bubblesort of two array
#include<iostream>
using namespace std;
int main()
{
int a[50],n,i,j,temp;
cout<<"Enter the size of array: ";
cin>>n;
cout<<"Enter the array elements: ";
for(i=0;i<n;++i)
cin>>a[i];
for(i=1;i<n;++i)
{
for(j=0;j<(n-i);++j)
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
cout<<"Array after bubble sort:";
for(i=0;i<n;++i)
cout<<" "<<a[i];
return 0;
}