forked from saransh79/LearnGit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request saransh79#153 from rohit010pro/main
Sorting algorithm added
- Loading branch information
Showing
3 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 <stdio.h> | ||
|
||
// BUBBLE SORT | ||
|
||
void bubble_sort(int arr[], int n) { | ||
int temp; | ||
|
||
for (int i = 0; i < n - 1; i++) | ||
{ | ||
for (int j = 0; j < n - 1 - i; j++) | ||
{ | ||
if (arr[j] > arr[j + 1]) | ||
{ | ||
temp = arr[j]; | ||
arr[j] = arr[j + 1]; | ||
arr[j + 1] = temp; | ||
} | ||
} | ||
} | ||
} | ||
|
||
// PRINT ARRAY | ||
|
||
void print_arr(int arr[], int n) { | ||
for (int i = 0; i < n; ++i) | ||
printf("%d ", arr[i]); | ||
} | ||
|
||
int main() | ||
{ | ||
int arr[] = { 80, 90, 100, 40, 50, 30, 20, 70, 60, 10}, n = 10; | ||
|
||
bubble_sort(arr, n); | ||
|
||
print_arr(arr, n); | ||
|
||
return 0; | ||
} |
This file contains 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,36 @@ | ||
#include <stdio.h> | ||
|
||
// INSERTION SORT | ||
|
||
void insertion_sort(int arr[], int n) { | ||
int key, j; | ||
for (int i = 1; i < n; i++) | ||
{ | ||
key = arr[i]; | ||
j = i - 1; | ||
while (j >= 0 && arr[j] > key) { | ||
arr[j + 1] = arr[j]; | ||
j--; | ||
} | ||
arr[j + 1] = key; | ||
} | ||
} | ||
|
||
|
||
// PRINT ARRAY | ||
|
||
void print_arr(int arr[], int n) { | ||
for (int i = 0; i < n; ++i) | ||
printf("%d ", arr[i]); | ||
} | ||
|
||
int main() | ||
{ | ||
int arr[] = { 80, 90, 100, 40, 50, 30, 20, 70, 60, 10}, n = 10; | ||
|
||
insertion_sort(arr, n); | ||
|
||
print_arr(arr, n); | ||
|
||
return 0; | ||
} |
This file contains 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,39 @@ | ||
#include <stdio.h> | ||
|
||
// SELECTION SORT | ||
|
||
void selection_sort(int arr[], int n) { | ||
int min_idx, temp; | ||
for (int i = 0; i < n - 1; i++) | ||
{ | ||
min_idx = i; | ||
for (int j = i + 1; j < n; j++) { | ||
if (arr[j] < arr[min_idx]) | ||
min_idx = j; | ||
} | ||
if (min_idx != i) { | ||
temp = arr[i]; | ||
arr[i] = arr[min_idx]; | ||
arr[min_idx] = temp; | ||
} | ||
} | ||
} | ||
|
||
|
||
// PRINT ARRAY | ||
|
||
void print_arr(int arr[], int n) { | ||
for (int i = 0; i < n; ++i) | ||
printf("%d ", arr[i]); | ||
} | ||
|
||
int main() | ||
{ | ||
int arr[] = { 80, 90, 100, 40, 50, 30, 20, 70, 60, 10}, n = 10; | ||
|
||
selection_sort(arr, n); | ||
|
||
print_arr(arr, n); | ||
|
||
return 0; | ||
} |