ALGORITHM
SelectionSort(A, n)
1. for i ← 0 to n-2 do
2. minIndex ← i
3. for j ← i+1 to n-1 do
4. if A[j] < A[minIndex] then
5. minIndex ← j
6. swap A[i] and A[minIndex]
Working of the Program
Input: Number of elements (n > 5000).
Generate Random Numbers: Fills the array using rand() % 10000.
Sort Using Selection Sort:
Finds the minimum in the unsorted part.
Swaps it with the current index.
Repeats for all elements.
Measure Time:
clock_t start = clock(); before sort
clock_t end = clock(); after sort.
Calculate time using time_taken = (end - start) /
CLOCKS_PER_SEC.
Output: Displays time taken to sort.
Free Memory
📉 Time Complexity
Time
Case
Complexity
Best
O(n²)
Case
Average O(n²)
Worst
O(n²)
Case
Even if the array is sorted, selection sort always makes the same number of
comparisons.
✅ Advantages
Simple to implement.
Performs well for small datasets.
Doesn’t require additional memory.
❌ Disadvantages
Inefficient for large datasets due to O(n²) time.
Slower than more advanced sorts (e.g., quicksort, mergesort).