Skip to content

Commit 898caa1

Browse files
committed
added sorting algorithms
1 parent daf84e2 commit 898caa1

File tree

5 files changed

+250
-0
lines changed

5 files changed

+250
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Question:Bubble Sort Algorithm
2+
#include <iostream>
3+
using namespace std;
4+
5+
int n;
6+
7+
void bubble_Sort(int a[])
8+
{
9+
10+
for(int i=0; i<n-1; i++)
11+
{
12+
int flag = 0; // Declaring a variable to check if elements have been sorted in this iteration.
13+
for(int j=0; j<n-1-i; j++)
14+
{
15+
if(a[j] > a[j+1])
16+
{
17+
swap(a[j+1], a[j]);
18+
flag = 1; // Set flag = 1 to signify that the element is sorted.c
19+
}
20+
}
21+
if (flag == 0)
22+
{
23+
// No elements have been swapped, which means that the array is sorted. End the algorithm.
24+
break;
25+
}
26+
}
27+
}
28+
29+
int main()
30+
{
31+
cout<<"Enter the size of the array"<<endl;
32+
cin >> n;
33+
int a[n];
34+
cout<<"Enter the elements of the array"<<endl;
35+
for(int i=0; i<n; i++)
36+
cin >> a[i];
37+
bubble_Sort(a);
38+
cout<<"The sorted array is : "<<endl;
39+
for (int i = 0; i < n; i++) {
40+
cout << a[i] << '\n';
41+
}
42+
return 0;
43+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Question: Heap Sorting Algorithm
2+
#include <iostream>
3+
4+
using namespace std;
5+
6+
// To heapify a subtree rooted with node i which is
7+
// an index in arr[]. n is size of heap
8+
void heapify(int arr[], int n, int i)
9+
{
10+
int largest = i; // Initialize largest as root
11+
int l = 2*i + 1; // left = 2*i + 1
12+
int r = 2*i + 2; // right = 2*i + 2
13+
14+
// If left child is larger than root
15+
if (l < n && arr[l] > arr[largest])
16+
largest = l;
17+
18+
// If right child is larger than largest so far
19+
if (r < n && arr[r] > arr[largest])
20+
largest = r;
21+
22+
// If largest is not root
23+
if (largest != i)
24+
{
25+
swap(arr[i], arr[largest]);
26+
27+
// Recursively heapify the affected sub-tree
28+
heapify(arr, n, largest);
29+
}
30+
}
31+
32+
// main function to do heap sort
33+
void heapSort(int arr[], int n)
34+
{
35+
// Build heap (rearrange array)
36+
for (int i = n / 2 - 1; i >= 0; i--)
37+
heapify(arr, n, i);
38+
39+
// One by one extract an element from heap
40+
for (int i=n-1; i>=0; i--)
41+
{
42+
// Move current root to end
43+
swap(arr[0], arr[i]);
44+
45+
// call max heapify on the reduced heap
46+
heapify(arr, i, 0);
47+
}
48+
}
49+
50+
/* A utility function to print array of size n */
51+
void printArray(int arr[], int n)
52+
{
53+
for (int i=0; i<n; ++i)
54+
cout << arr[i] << " ";
55+
cout << "\n";
56+
}
57+
58+
// Driver program
59+
int main()
60+
{
61+
int arr[] = {12, 11, 13, 5, 6, 7};
62+
int n = sizeof(arr)/sizeof(arr[0]);
63+
64+
heapSort(arr, n);
65+
66+
cout << "Sorted array is \n";
67+
printArray(arr, n);
68+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Question:Insertion Sort Algorithm
2+
#include <iostream>
3+
using namespace std;
4+
5+
int n;
6+
void insertionSort(int A[])
7+
{
8+
int key;
9+
for(int i=0;i<n;i++)
10+
{
11+
key = A[i];
12+
int j = i-1;
13+
while(j>=0 and key<A[j])
14+
{
15+
A[j+1] = A[j];
16+
j--;
17+
}
18+
A[j+1]=key;
19+
}
20+
}
21+
22+
int main()
23+
{
24+
std::cout << "Enter the array length: ";
25+
std::cin >> n;
26+
int A[n];
27+
std::cout << "Enter the array elements :" << '\n';
28+
for(int i=0; i<n; i++)
29+
cin >> A[i];
30+
insertionSort(A);
31+
std::cout << "Sorted Array :" << '\n';
32+
for(int i=0;i<n;i++)
33+
cout << A[i] << endl;
34+
return 0;
35+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Question:Program for quick sort implementation in C++.
2+
3+
#include <iostream>
4+
#include <vector>
5+
6+
int partition(std::vector<int> &A, int start, int end)
7+
{
8+
int temp;
9+
int pivot = A[end];
10+
int partitionIndex = start; // set partition index as start initially
11+
for(int i=start; i<end; i++)
12+
{
13+
if(A[i] <= pivot)
14+
{
15+
std::swap(A[i], A[partitionIndex]);
16+
partitionIndex++;
17+
}
18+
}
19+
std::swap(A[partitionIndex], A[end]); // partition index
20+
21+
return partitionIndex;
22+
}
23+
24+
void swapElements(int* elementOne, int* elementTwo)
25+
{
26+
int temp = *elementOne;
27+
*elementOne = *elementTwo;
28+
*elementTwo = temp;
29+
}
30+
std::vector<int> quick_sort(std::vector<int> &A, int start, int end)
31+
{
32+
/* std::cout<<"Current list : \n";
33+
for(int i=0;i<A.size();i++)
34+
{
35+
std::cout<<A[i]<<" ";
36+
}
37+
std::cout<<"\nSorted list : \n";
38+
*/
39+
40+
if(start<end)
41+
{
42+
int partitionIndex = partition(A,start,end);
43+
quick_sort(A,start,partitionIndex-1);
44+
quick_sort(A,partitionIndex+1, end);
45+
}
46+
47+
/* for(int i=0;i<A.size();i++)
48+
{
49+
std::cout<<A[i]<<" ";
50+
}
51+
*/ return A;
52+
}
53+
54+
int main()
55+
{
56+
std::cout<<"\nTest\n";
57+
std::vector<int> arr={10,2,23,-4,235,56,2,6,5,5,5,23,-4,346,-56,81,43,654,435,-54};
58+
quick_sort(arr, 0, arr.size()-1);
59+
std::cout<<"\n\n\n";
60+
for(int i=0;i<arr.size();i++)
61+
{
62+
std::cout<<arr[i]<<" ";
63+
}
64+
return 0;
65+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Question:Selection Sot Algorithm
2+
#include<iostream>
3+
using namespace std;
4+
void swapping(int &a, int &b) {
5+
int temp;
6+
temp = a;
7+
a = b;
8+
b = temp;
9+
}
10+
void display(int *array, int size) {
11+
for(int i = 0; i<size; i++)
12+
cout << array[i] << " ";
13+
cout << endl;
14+
}
15+
void selectionSort(int *array, int size) {
16+
int i, j, imin;
17+
for(i = 0; i<size-1; i++) {
18+
imin = i;
19+
for(j = i+1; j<size; j++)
20+
if(array[j] < array[imin])
21+
imin = j;
22+
swap(array[i], array[imin]);
23+
}
24+
}
25+
int main() {
26+
int n;
27+
cout << "Enter the number of elements: ";
28+
cin >> n;
29+
int arr[n];
30+
cout << "Enter elements:" << endl;
31+
for(int i = 0; i<n; i++) {
32+
cin >> arr[i];
33+
}
34+
cout << "Array before Sorting: ";
35+
display(arr, n);
36+
selectionSort(arr, n);
37+
cout << "Array after Sorting: ";
38+
display(arr, n);
39+
}

0 commit comments

Comments
 (0)