Skip to content

Commit 0e3e7f4

Browse files
committed
Added all the sorting methods mentioned in the Sorting folder (in C++/cpp extension)
1 parent 48c1780 commit 0e3e7f4

8 files changed

+449
-0
lines changed

Sorting/bubblesort.cpp

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
void swap(int *xp, int *yp)
5+
{
6+
int temp = *xp;
7+
*xp = *yp;
8+
*yp = temp;
9+
}
10+
11+
// A function to implement bubble sort
12+
void bubbleSort(int arr[], int n)
13+
{
14+
int i, j;
15+
for (i = 0; i < n-1; i++)
16+
for (j = 0; j < n-i-1; j++)
17+
if (arr[j] > arr[j+1])
18+
swap(&arr[j], &arr[j+1]);
19+
}
20+
21+
/* Function to print an array */
22+
void printArray(int arr[], int size)
23+
{
24+
int i;
25+
for (i = 0; i < size; i++)
26+
cout << arr[i] << " ";
27+
cout << endl;
28+
}
29+
30+
int main()
31+
{
32+
int arr[] = {64, 34, 25, 12, 22, 11, 90};//array is predefined here, please take input from the user to work accordingly by modifying the code
33+
int n = sizeof(arr)/sizeof(arr[0]);
34+
bubbleSort(arr, n);
35+
cout<<"Sorted array: \n";
36+
printArray(arr, n);
37+
return 0;
38+
}

Sorting/countsort.cpp

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include <bits/stdc++.h>
2+
#include <string.h>
3+
using namespace std;
4+
#define RANGE 255
5+
6+
// The main function that sort
7+
// the given string arr[] in
8+
// alphabatical order
9+
void countSort(char arr[])
10+
{
11+
// The output character array
12+
// that will have sorted arr
13+
char output[strlen(arr)];
14+
15+
// Create a count array to store count of inidividul
16+
// characters and initialize count array as 0
17+
int count[RANGE + 1], i;
18+
memset(count, 0, sizeof(count));
19+
20+
// Store count of each character
21+
for (i = 0; arr[i]; ++i)
22+
++count[arr[i]];
23+
24+
// Change count[i] so that count[i] now contains actual
25+
// position of this character in output array
26+
for (i = 1; i <= RANGE; ++i)
27+
count[i] += count[i - 1];
28+
29+
// Build the output character array
30+
for (i = 0; arr[i]; ++i) {
31+
output[count[arr[i]] - 1] = arr[i];
32+
--count[arr[i]];
33+
}
34+
35+
/*
36+
For Stable algorithm
37+
for (i = sizeof(arr)-1; i>=0; --i)
38+
{
39+
output[count[arr[i]]-1] = arr[i];
40+
--count[arr[i]];
41+
}
42+
*/
43+
44+
// Copy the output array to arr, so that arr now
45+
// contains sorted characters
46+
for (i = 0; arr[i]; ++i)
47+
arr[i] = output[i];
48+
}
49+
50+
int main()
51+
{
52+
char arr[] = "geeksforgeeks";
53+
54+
countSort(arr);
55+
56+
cout << "Sorted character array is " << arr;
57+
return 0;
58+
}

Sorting/heapsort.cpp

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
// To heapify a subtree rooted with node i which is
6+
// an index in arr[]. n is size of heap
7+
void heapify(int arr[], int n, int i)
8+
{
9+
int largest = i; // Initialize largest as root
10+
int l = 2*i + 1; // left = 2*i + 1
11+
int r = 2*i + 2; // right = 2*i + 2
12+
13+
// If left child is larger than root
14+
if (l < n && arr[l] > arr[largest])
15+
largest = l;
16+
17+
// If right child is larger than largest so far
18+
if (r < n && arr[r] > arr[largest])
19+
largest = r;
20+
21+
// If largest is not root
22+
if (largest != i)
23+
{
24+
swap(arr[i], arr[largest]);
25+
26+
// Recursively heapify the affected sub-tree
27+
heapify(arr, n, largest);
28+
}
29+
}
30+
31+
// main function to do heap sort
32+
void heapSort(int arr[], int n)
33+
{
34+
// Build heap (rearrange array)
35+
for (int i = n / 2 - 1; i >= 0; i--)
36+
heapify(arr, n, i);
37+
38+
// One by one extract an element from heap
39+
for (int i=n-1; i>0; i--)
40+
{
41+
// Move current root to end
42+
swap(arr[0], arr[i]);
43+
44+
// call max heapify on the reduced heap
45+
heapify(arr, i, 0);
46+
}
47+
}
48+
49+
/* A utility function to print array of size n */
50+
void printArray(int arr[], int n)
51+
{
52+
for (int i=0; i<n; ++i)
53+
cout << arr[i] << " ";
54+
cout << "\n";
55+
}
56+
57+
// Driver program
58+
int main()
59+
{
60+
int arr[] = {12, 11, 13, 5, 6, 7};
61+
int n = sizeof(arr)/sizeof(arr[0]);
62+
63+
heapSort(arr, n);
64+
65+
cout << "Sorted array is \n";
66+
printArray(arr, n);
67+
}

Sorting/insertionsort.cpp

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include<iostream>
2+
using namespace std;
3+
void display(int *array, int size) {
4+
for(int i = 0; i<size; i++)
5+
cout << array[i] << " ";
6+
cout << endl;
7+
}
8+
void insertionSort(int *array, int size) {
9+
int key, j;
10+
for(int i = 1; i<size; i++) {
11+
key = array[i];//take value
12+
j = i;
13+
while(j > 0 && array[j-1]>key) {
14+
array[j] = array[j-1];
15+
j--;
16+
}
17+
array[j] = key; //insert in right place
18+
}
19+
}
20+
int main() {
21+
int n;
22+
cout << "Enter the number of elements: ";
23+
cin >> n;
24+
int arr[n]; //create an array with given number of elements
25+
cout << "Enter elements:" << endl;
26+
for(int i = 0; i<n; i++) {
27+
cin >> arr[i];
28+
}
29+
cout << "Array before Sorting: ";
30+
display(arr, n);
31+
insertionSort(arr, n);
32+
cout << "Array after Sorting: ";
33+
display(arr, n);
34+
}

Sorting/mergesort.cpp

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include<iostream>
2+
using namespace std;
3+
void swapping(int &a, int &b) { //swap the content of a and b
4+
int temp;
5+
temp = a;
6+
a = b;
7+
b = temp;
8+
}
9+
void display(int *array, int size) {
10+
for(int i = 0; i<size; i++)
11+
cout << array[i] << " ";
12+
cout << endl;
13+
}
14+
void merge(int *array, int l, int m, int r) {
15+
int i, j, k, nl, nr;
16+
//size of left and right sub-arrays
17+
nl = m-l+1; nr = r-m;
18+
int larr[nl], rarr[nr];
19+
//fill left and right sub-arrays
20+
for(i = 0; i<nl; i++)
21+
larr[i] = array[l+i];
22+
for(j = 0; j<nr; j++)
23+
rarr[j] = array[m+1+j];
24+
i = 0; j = 0; k = l;
25+
//marge temp arrays to real array
26+
while(i < nl && j<nr) {
27+
if(larr[i] <= rarr[j]) {
28+
array[k] = larr[i];
29+
i++;
30+
}else{
31+
array[k] = rarr[j];
32+
j++;
33+
}
34+
k++;
35+
}
36+
while(i<nl) { //extra element in left array
37+
array[k] = larr[i];
38+
i++; k++;
39+
}
40+
while(j<nr) { //extra element in right array
41+
array[k] = rarr[j];
42+
j++; k++;
43+
}
44+
}
45+
void mergeSort(int *array, int l, int r) {
46+
int m;
47+
if(l < r) {
48+
int m = l+(r-l)/2;
49+
// Sort first and second arrays
50+
mergeSort(array, l, m);
51+
mergeSort(array, m+1, r);
52+
merge(array, l, m, r);
53+
}
54+
}
55+
int main() {
56+
int n;
57+
cout << "Enter the number of elements: ";
58+
cin >> n;
59+
int arr[n]; //create an array with given number of elements
60+
cout << "Enter elements:" << endl;
61+
for(int i = 0; i<n; i++) {
62+
cin >> arr[i];
63+
}
64+
cout << "Array before Sorting: ";
65+
display(arr, n);
66+
mergeSort(arr, 0, n-1); //(n-1) for last index
67+
cout << "Array after Sorting: ";
68+
display(arr, n);
69+
}

Sorting/quicksort.cpp

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include <iostream>
2+
using namespace std;
3+
// Swap two elements - Utility function
4+
void swap(int* a, int* b)
5+
{
6+
int t = *a;
7+
*a = *b;
8+
*b = t;
9+
}
10+
11+
// partition the array using last element as pivot
12+
int partition (int arr[], int low, int high)
13+
{
14+
int pivot = arr[high]; // pivot
15+
int i = (low - 1);
16+
17+
for (int j = low; j <= high- 1; j++)
18+
{
19+
//if current element is smaller than pivot, increment the low element
20+
//swap elements at i and j
21+
if (arr[j] <= pivot)
22+
{
23+
i++; // increment index of smaller element
24+
swap(&arr[i], &arr[j]);
25+
}
26+
}
27+
swap(&arr[i + 1], &arr[high]);
28+
return (i + 1);
29+
}
30+
31+
//quicksort algorithm
32+
void quickSort(int arr[], int low, int high)
33+
{
34+
if (low < high)
35+
{
36+
//partition the array
37+
int pivot = partition(arr, low, high);
38+
39+
//sort the sub arrays independently
40+
quickSort(arr, low, pivot - 1);
41+
quickSort(arr, pivot + 1, high);
42+
}
43+
}
44+
45+
void displayArray(int arr[], int size)
46+
{
47+
int i;
48+
for (i=0; i < size; i++)
49+
cout<<arr[i]<<"\t";
50+
51+
}
52+
53+
int main()
54+
{
55+
int arr[] = {12,23,3,43,51,35,19,45};
56+
int n = sizeof(arr)/sizeof(arr[0]);
57+
cout<<"Input array"<<endl;
58+
displayArray(arr,n);
59+
cout<<endl;
60+
quickSort(arr, 0, n-1);
61+
cout<<"Array sorted with quick sort"<<endl;
62+
displayArray(arr,n);
63+
return 0;
64+
}

0 commit comments

Comments
 (0)