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
102 changes: 102 additions & 0 deletions Sorting Algorithms/merge-sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#include<stdlib.h>
#include<stdio.h>
#include<iostream>
using namespace std;

// Merges two subarrays of arr[].
// First subarray is arr[l..m]
// Second subarray is arr[m+1..r]
void merge(int arr[], int l, int m, int r)
{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;

/* create temp arrays */
int L[n1], R[n2];

/* Copy data to temp arrays L[] and R[] */
for (i = 0; i < n1; i++)
L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1+ j];

/* Merge the temp arrays back into arr[l..r]*/
i = 0; // Initial index of first subarray
j = 0; // Initial index of second subarray
k = l; // Initial index of merged subarray
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}

while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}

while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}


void mergeSort(int arr[], int l, int r)
{
if (l < r)
{
int m = l+(r-l)/2;
mergeSort(arr, l, m);
mergeSort(arr, m+1, r);

merge(arr, l, m, r);
}
}


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

int main()
{
int arr[10];
int n;
cout<<"Enter the size of array: ";
cin>>n;
cout<<"Enter the elements: ";

for(int i=0;i<n;i++){
cin>>arr[i];
}



cout<<"Given array is: "<<endl;
printArray(arr, n);

mergeSort(arr, 0, n - 1);

cout<<endl<<"Sorted Array is: "<<endl;
printArray(arr, n);
return 0;
}
72 changes: 72 additions & 0 deletions Sorting Algorithms/quick-sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#include <iostream>
using namespace std;


void swap(int* a, int* b)
{
int t = *a;
*a = *b;
*b = t;
}


int partition (int arr[], int low, int high)
{
int pivot = arr[high]; // pivot
int i = (low - 1); // Index of smaller element

for (int j = low; j <= high - 1; j++)
{
// If current element is smaller than the pivot
if (arr[j] < pivot)
{
i++; // increment index of smaller element
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}


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

int pi = partition(arr, low, high);

quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}


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


int main()
{
int arr[10];
int n;
cout<<"Enter the size of array: ";
cin>>n;
cout<<"Enter the elements of array: ";
for(int i=0;i<n;i++){
cin>>arr[i];
}
cout<<"Enter the index of element you want to pivot: ";
int pivot;
cin>>pivot;
swap(&arr[pivot],&arr[n-1]);
quickSort(arr, 0, n - 1);
cout << "Sorted array: \n";
display(arr, n);
return 0;
}