Skip to content
This repository has been archived by the owner on May 29, 2024. It is now read-only.

Add complexity documentation in c files for quick-sort.c, selection-sort.c and merge-sort and commentary for selection-sort.c #1322

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
2 changes: 2 additions & 0 deletions algorithms/C/sorting/merge-sort.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,5 @@ void mergeSort(int a[], int l, int r)
merging(a, l, m, r);
}
}

// Time Complexity: O(n log n)
4 changes: 4 additions & 0 deletions algorithms/C/sorting/quick-sort.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,8 @@ int main()
/*
Output:
1 2 3 4 5 6

Complexity:
Worst case time complexity = O(n^2)
Average case time complexity = O(n log n) = Best case time complexity
*/
21 changes: 18 additions & 3 deletions algorithms/C/sorting/selection-sort.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@

#include <stdio.h>


// Function to swap two elements
void swap(int *x, int *y)
{
int t = *x;
*x = *y;
*y = t;
}


// Function to perform selection sort
void selectionSort(int a[], int n)
{
int i, j;
Expand All @@ -19,7 +21,8 @@ void selectionSort(int a[], int n)
}
}
}


// Function to print an array
void printArray(int a[], int size)
{
int i;
Expand All @@ -28,6 +31,7 @@ void printArray(int a[], int size)
}
}

// Main function
int main()
{
int a[] = {64, 25, 25, 12, 22, 11, 90};
Expand All @@ -39,3 +43,14 @@ int main()
printArray(a, n);
return 0;
}

/*
Output:
Initial array:
64 25 25 12 22 11 90
Sorted array:
11 12 22 25 25 64 90

Complexity:
Worst case time complexity = Best case time complexity = Average case time complexity = O(n^2)
*/