Skip to content
This repository has been archived by the owner on Oct 2, 2018. It is now read-only.

Added Sorting Algo's #40

Merged
merged 5 commits into from
Oct 20, 2017
Merged
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
46 changes: 46 additions & 0 deletions cpp/insertion_sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <iostream>
using namespace std;

// perform insertion sort on arr[]
void insertionSort(int arr[], int n)
{
// Start from second element (element at index 0
// is already sorted)
for (int i = 1; i < n; i++)
{
int value = arr[i];
int j = i;

// Find the index j within the sorted subset arr[0..i-1]
// where element arr[i] belongs
while (j > 0 && arr[j - 1] > value)
{
arr[j] = arr[j - 1];
j--;
}
// Note that subarray arr[j..i-1] is shifted to
// the right by one position i.e. arr[j+1..i]

arr[j] = value;
}
}

// Function to print n elements of the array arr
void printArray(int arr[], int n)
{
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
}

int main()
{
int arr[] = { 3, 8, 5, 4, 1, 9, -2 };
int n = sizeof(arr) / sizeof(arr[0]);

insertionSort(arr, n);

// print the sorted array
printArray(arr, n);

return 0;
}
44 changes: 44 additions & 0 deletions cpp/insertion_sort_recursive.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <iostream>
using namespace std;

// recursive function to perform insertion sort on subarray arr[i..n]
void insertionSort(int arr[], int i, int n)
{
int value = arr[i];
int j = i;
// Find index j within the sorted subset arr[0..i-1]
// where element arr[i] belongs
while (j > 0 && arr[j - 1] > value)
{
arr[j] = arr[j - 1];
j--;
}
arr[j] = value;
// Note that subarray arr[j..i-1] is shifted to
// the right by one position i.e. arr[j+1..i]

if (i + 1 <= n)
insertionSort(arr, i + 1, n);
}

// Function to print n elements of the array arr
void printArray(int arr[], int n)
{
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
}

int main()
{
int arr[] = { 3, 8, 5, 4, 1, 9, -2 };
int n = sizeof(arr) / sizeof(arr[0]);

// Start from second element (element at index 0
// is already sorted)
insertionSort(arr, 1, n - 1);

// print the sorted array
printArray(arr, n);

return 0;
}
43 changes: 43 additions & 0 deletions cpp/selection_sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <iostream>
using namespace std;

// perform selection sort on arr[]
void selectionSort(int arr[], int n)
{
// run (n - 1) times
for (int i = 0; i < n - 1; i++)
{
// find the minimum element in the unsorted subarray[i..n-1]
// and swap it with arr[i]
int min = i;

for (int j = i + 1; j < n; j++)
{
// if arr[j] element is less, then it is the new minimum
if (arr[j] < arr[min])
min = j; // update index of min element
}

// swap the minimum element in subarray[i..n-1] with arr[i]
swap(arr[min], arr[i]);
}
}

// Function to print n elements of the array arr
void printArray(int arr[], int n)
{
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
}

// main function
int main()
{
int arr[] = { 3, 5, 8, 4, 1, 9, -2 };
int n = sizeof(arr) / sizeof(arr[0]);

selectionSort(arr, n);
printArray(arr, n);

return 0;
}
41 changes: 41 additions & 0 deletions cpp/selection_sort_recursive.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <iostream>
using namespace std;

// recursive function to perform selection sort on subarray arr[i..n-1]
void selectionSort(int arr[], int i, int n)
{
// find the minimum element in the unsorted subarray[i..n-1]
// and swap it with arr[i]
int min = i;
for (int j = i + 1; j < n; j++)
{
// if arr[j] element is less, then it is the new minimum
if (arr[j] < arr[min])
min = j; // update index of min element
}

// swap the minimum element in subarray[i..n-1] with arr[i]
swap(arr[min], arr[i]);

if (i + 1 < n)
selectionSort(arr, i + 1, n);
}

// Function to print n elements of the array arr
void printArray(int arr[], int n)
{
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
}

// main function
int main()
{
int arr[] = { 3, 5, 8, 4, 1, 9, -2 };
int n = sizeof(arr) / sizeof(arr[0]);

selectionSort(arr, 0, n);
printArray(arr, n);

return 0;
}