- Notifications
You must be signed in to change notification settings - Fork370
Added Top 5 Sorting Algorithms#748
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
RonakSurana-2001 wants to merge1 commit intocodemistic:mainChoose a base branch fromRonakSurana-2001:main
base:main
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Open
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
46 changes: 46 additions & 0 deletionsSorting_Algorithms/Bubble_Sort.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
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 deletionsSorting_Algorithms/Insertion_Sort.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
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 deletionsSorting_Algorithms/Merge_Sort.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
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 deletionsSorting_Algorithms/Quick_Sort.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
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 deletionsSorting_Algorithms/Selection_Sort.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
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; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.