Selection Sort is a simple comparison-based sorting algorithm that repeatedly selects the smallest element and places it in sorted order, with a time complexity of O(n²) in all cases. The algorithm is inefficient for large datasets but works well for small arrays. Better sorting algorithms like QuickSort and MergeSort exist for larger inputs.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
13 views7 pages
Selection Sort Presentation
Selection Sort is a simple comparison-based sorting algorithm that repeatedly selects the smallest element and places it in sorted order, with a time complexity of O(n²) in all cases. The algorithm is inefficient for large datasets but works well for small arrays. Better sorting algorithms like QuickSort and MergeSort exist for larger inputs.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 7
Selection Sort in C
Implementation, Explanation, and
Example Introduction to Selection Sort • • Selection Sort is a simple comparison-based sorting algorithm. • • It repeatedly selects the smallest element and places it in sorted order. • • Time Complexity: O(n²) in all cases. • • Not suitable for large datasets. Algorithm Explanation • 1. Find the minimum element in the unsorted portion. • 2. Swap it with the first element of the unsorted portion. • 3. Move the boundary of the sorted portion one step right. • 4. Repeat until the array is fully sorted. Selection Sort - C Code • #include <stdio.h> • #include <stdlib.h> • #include <time.h>
• void selectionSort(int arr[], int n) {
• for (int i = 0; i < n - 1; i++) { • int minIndex = i; • for (int j = i + 1; j < n; j++) { • if (arr[j] < arr[minIndex]) { Example Walkthrough • Given Array: [64, 25, 12, 22, 11] • Step 1: Select min (11), swap with 64 → [11, 25, 12, 22, 64] • Step 2: Select min (12), swap with 25 → [11, 12, 25, 22, 64] • Step 3: Select min (22), swap with 25 → [11, 12, 22, 25, 64] • Step 4: Select min (25), no swap → [11, 12, 22, 25, 64] • Sorted Array: [11, 12, 22, 25, 64] Execution Time Analysis • • The program measures time using clock() from time.h. • • Formula: (end_time - start_time) / CLOCKS_PER_SEC * 1000 (in ms). • • Time Complexity: O(n²), inefficient for large inputs. • • Example Output: • - Input: 5000 elements • - Time taken: ~120ms Conclusion • ✔ Selection Sort is simple but inefficient for large datasets. • ✔ Works well for small arrays but better algorithms exist (e.g., QuickSort, MergeSort). • ✔ Time complexity remains O(n²) in all cases. • ✔ Sorting time increases significantly for large inputs.