Skip to content
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
33 changes: 33 additions & 0 deletions BubbleSorting.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <stdio.h>
int bubbleSort(int arr[], int size)
{
for (int i = 0; i < size - 1; i++)
{ //this loop is for each pass and pass is size - 1
for (int j = 0; j < size - i - 1; j++)
{ //this loop is for each comparison
if (arr[j] > arr[j + 1])
{ //this code is for swapping
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
void printArray(int arr[], int size)
{
for (int i = 0; i < size; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}
int main()
{
int data[] = {24, 11, 99, 23, 55, 64};
int size = sizeof(data) / sizeof(data[0]);
bubbleSort(data, size);
printf("Sorted Array in Ascening Order : ");
printArray(data, size);
return 0;
}
40 changes: 40 additions & 0 deletions SelectionSorting.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//Selection Sort Algorithm
#include <stdio.h>
void selectionSort(int arr[], int size)
{ //this loop is for accessing passes
for (int i = 0; i < size - 1; i++)
{ //this loop is for comparison
int min = i;
for (int j = i + 1; j < size; j++)
{
if (arr[j] < arr[min])
{
min = j;
}
}
if (min != i)
{
//this loop is for swapping
int temp = arr[i];
arr[i] = arr[min];
arr[min] = temp;
}
}
}
void printArray(int arr[], int size)
{
for (int i = 0; i < size; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}
int main()
{
int arr[] = {7, 4, 9, 42, 31, 6, 20};
int size = sizeof(arr) / sizeof(arr[0]);
selectionSort(arr, size);
printf("The Sorted Array is : ");
printArray(arr, size);
return 0;
}