Image for post
Image for post

Bubble Sort

Image for post
Image for post
Bubble Sort Algorithm
/*
This program sorts an array of elements using the bubble sort algorithm
By: randerson112358

Output:
Enter total number(s) of elements: 4
Enter the 4 elements: 1 5 4 3
After Sorting: 1 3 4 5

*/

#include <stdio.h>

int BubbleSort(int size, int *array);

int main(void){

int size, i, array[20];

printf("Enter total number(s) of elements: ");
scanf("%d", &size);

printf("Enter the %d elements: ", size);
for(i=0; i<size; i++){
scanf("%d", &array[i]);
}

//Run the Bubble Sort Algorithm to sort the list of elements
BubbleSort(size, array);

printf("After Sorting: ");
for(i=0; i<size; i++){
printf(" %d", array[i]);
}

printf("\n");
system("pause"); // uncomment if you are not using Windows OS
return 0;
}

int BubbleSort(int size, int *array){

int i, j, temp;

//Bubble sorting algorthm
for(i=size-2; i>= 0; i--){
for(j=0; j<=i; j++){

//Swap
if(array[j] > array[j+1]){
temp = array[j];
array[j] = array[j+1];
array[j+1]= temp;
}
}
}

return 1;
}

Heap Sort

Image for post
Image for post
Heap Sort

Insertion Sort

/*
This program sorts an array of elements using the insertion sort algorithm
By:randerson112358

Output:
Enter total elements: 3
Enter 3 elements: 9 4 8
After Sorting: 4 8 9
*/
#include < stdio.h >int insertionSort(int size, int *array);int main(){

int size, i , array[21];

printf("Enter total number of elements: ");
scanf("%d", &size);

printf("Enter %d elements: ", size);
for(i=0; i < size; i++)
scanf("%d", &array[i]);

//Start sorting the array using the insertion sort algorithm
insertionSort(size, array);

printf("After Sorting: ");
for(i=0; i < size; i++)
printf(" %d", array[i]);

printf("\n");
system("pause"); //Comment this our if you are not using a Windows OS
return 0;
}
int insertionSort(int size, int *array){

int i, j;
int temp;

//Insertion Sort
for(i=1; i < size; i++){

temp=array[i];
j= i-1;

while( (temp < array[j]) && (j >= 0)){

//swap
array[j+1] = array[j];
j= j-1;
}

array[j+1] = temp;
}

return 1;
}

Selection Sort

/*
This program sorts an array of elements using the selection sort algorithm
By:randerson112358

Output:
Enter the total number of elements: 5
Enter 5 elements: 5 6 1 20 7
The array after sorting is: 1 5 6 7 20

How it works:
5 6 1 20 7
1 6 5 20 7 (min = 1, swapped with 5)
1 5 6 20 7 (min = 5, swapped with 6)
1 5 6 20 7 (min =6, no swapping needed)
1 5 6 7 20 (min = 7, swapped with 20)
1 5 6 7 20 (min=20, we have reached the end of the list)



*/

#include<stdio.h>

int SelectionSort(int size, int *a);

int main(){

int size, i, array[21];

printf("Enter the total number of elements: ");
scanf("%d", &size);

printf("Enter %d elements: ", size);
for(i=0; i<size; i++)
scanf("%d", &array[i]);

//Sort the array using the selection sort algorithm
SelectionSort(size, array);

printf("The array after sorting is: ");
for(i=0; i<size; i++)
printf(" %d", array[i]);

return 0;
}

int SelectionSort(int size, int *a){

/* a[0] to a[n-1] is the array to sort */
int i,j;
int n;

/* advance the position through the entire array */
/* (could do j < n-1 because single element is also min element) */
for (j = 0; j < size-1; j++)
{
/* find the min element in the unsorted a[j .. n-1] */

/* assume the min is the first element */
int iMin = j;
/* test against elements after j to find the smallest */
for (i = j+1; i < size; i++) {
/* if this element is less, then it is the new minimum */
if (a[i] < a[iMin]) {
/* found new minimum; remember its index */
iMin = i;
}
}

if(iMin != j)
{
//swap(a[j], a[iMin]);
int temp = a[j];
a[j] = a[iMin];
a[iMin] = temp;
}
}

return 1;
}

Merge Sort

Image for post
Image for post
Merge Sort

Check Out the following for more content / videos on Algorithms and Algorithm Analysis:

YouTube Channel:

Image for post
Image for post

Computer Science Website:

Image for post
Image for post

Udemy Videos on Recurrence Relation:

Image for post
Image for post

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store