0% found this document useful (0 votes)
16 views2 pages

Selection Sort Algorithm Explained

The document outlines the Selection Sort algorithm, detailing its implementation and time complexity, which is O(n²) for best, average, and worst cases. It describes the process of generating random numbers, sorting the array, measuring the time taken, and displaying the output. The algorithm is simple and memory-efficient but inefficient for large datasets compared to advanced sorting algorithms like quicksort and mergesort.

Uploaded by

spoorthiks645
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views2 pages

Selection Sort Algorithm Explained

The document outlines the Selection Sort algorithm, detailing its implementation and time complexity, which is O(n²) for best, average, and worst cases. It describes the process of generating random numbers, sorting the array, measuring the time taken, and displaying the output. The algorithm is simple and memory-efficient but inefficient for large datasets compared to advanced sorting algorithms like quicksort and mergesort.

Uploaded by

spoorthiks645
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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).

You might also like