Skip to content

Commit

Permalink
Merge pull request saransh79#153 from rohit010pro/main
Browse files Browse the repository at this point in the history
Sorting algorithm added
  • Loading branch information
saransh79 committed Oct 12, 2022
2 parents af9885c + 6a71d54 commit 18a8307
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 0 deletions.
38 changes: 38 additions & 0 deletions Sorting Programs/bubble_sort.c
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;
}
36 changes: 36 additions & 0 deletions Sorting Programs/insertion_sort.c
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;
}
39 changes: 39 additions & 0 deletions Sorting Programs/selection_sort.c
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;
}

0 comments on commit 18a8307

Please sign in to comment.