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

Added Top 5 Sorting Algorithms #748

Open
wants to merge 1 commit into
base: main
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
46 changes: 46 additions & 0 deletions Sorting_Algorithms/Bubble_Sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <bits/stdc++.h>
using namespace std;
void bubbleSort(int arr[], int n);
void printArray(int arr[], int n);
int main()
{
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
bubbleSort(arr, n);
}
void bubbleSort(int arr[], int n)
{
int flag;
for (int i = 0; i < n; i++)
{
flag = 0;
for (int j = 0; j < n - 1; j++)
{
if (arr[j] > arr[j + 1])
{
arr[j] ^= arr[j + 1];
arr[j + 1] ^= arr[j];
arr[j] ^= arr[j + 1];
flag = 1;
}
}
if (flag == 0)
{
break;
}
}
printArray(arr, n);
}
void printArray(int arr[], int n)
{
for (int i = 0; i < n; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}
38 changes: 38 additions & 0 deletions Sorting_Algorithms/Insertion_Sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <bits/stdc++.h>
using namespace std;
void insertionSort(int arr[],int n);
void printArray(int arr[], int n);
int main()
{
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
insertionSort(arr, n);
}
void insertionSort(int arr[],int n)
{
for(int i=1;i<n;i++)
{
int value=arr[i];
int hole=i;
while(hole>0 && arr[hole-1]>value)
{
arr[hole]=arr[hole-1];
hole--;
}
arr[hole]=value;
}
printArray(arr, n);
}
void printArray(int arr[], int n)
{
for (int i = 0; i < n; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}
76 changes: 76 additions & 0 deletions Sorting_Algorithms/Merge_Sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include <bits/stdc++.h>
using namespace std;
void mergeSort(int arr[], int n);
void Merge(int left[], int right[], int arr[], int x, int y,int n);
void printArray(int arr[], int n);
int main()
{
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
mergeSort(arr, n);
printArray(arr,n);
}
void mergeSort(int arr[], int n)
{
if (n < 2)
{
return;
}
int mid = n / 2;
int left[mid];
int right[n - mid];
for (int i = 0; i < mid; i++)
{
left[i] = arr[i];
}
for (int i = mid; i < n; i++)
{
right[i - mid] = arr[i];
}
mergeSort(left, mid);
mergeSort(right, n - mid);
Merge(left, right, arr, mid, n - mid,n);
}
void Merge(int left[], int right[], int arr[], int x, int y,int n)
{
int i = 0, j = 0, k = 0;
while (i < x && j < y)
{
if (left[i] <= right[j])
{
arr[k] = left[i];
i++;
}
else
{
arr[k] = right[j];
j++;
}
k++;
}
while (i < x)
{
arr[k] = left[i];
i++;
k++;
}
while (j < y)
{
arr[k] = right[j];
j++;
k++;
}
}
void printArray(int arr[], int n)
{
for (int i = 0; i < n; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}
52 changes: 52 additions & 0 deletions Sorting_Algorithms/Quick_Sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <bits/stdc++.h>
using namespace std;
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
void printArray(int arr[], int n)
{
for (int i = 0; i < n; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}
int partition(int arr[], int start, int end)
{
int pivot = arr[end];
int pIndex = start - 1;
for (int i = start; i <= end - 1; i++)
{
if (arr[i] < pivot)
{
pIndex++;
swap(&arr[pIndex], &arr[i]);
}
}
swap(&arr[pIndex + 1], &arr[end]);
return pIndex + 1;
}
void quickSort(int arr[], int start, int end)
{
if (start < end)
{
int pIndex = partition(arr, start, end);
quickSort(arr, start, pIndex - 1);
quickSort(arr, pIndex + 1, end);
}
}
int main()
{
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
quickSort(arr, 0, n - 1);
printArray(arr, n);
}
44 changes: 44 additions & 0 deletions Sorting_Algorithms/Selection_Sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <bits/stdc++.h>
using namespace std;
void selectionSort(int arr[], int n);
void printArray(int arr[], int n);
int main()
{
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
selectionSort(arr, n);
}
void selectionSort(int arr[], int n)
{
for (int i = 0; i < n - 1; i++)
{
int min = i;
for (int j = i + 1; j < n; j++)
{
if (arr[j] <= arr[i] && arr[j] <= arr[min])
{
min = j;
}
}
if (min != i)
{
arr[i] ^= arr[min];
arr[min] ^= arr[i];
arr[i] ^= arr[min];
}
}
printArray(arr, n);
}
void printArray(int arr[], int n)
{
for (int i = 0; i < n; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}