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

Selection Sort

The document describes the selection sort algorithm, which sorts an array by repeatedly finding the minimum element from the unsorted part and placing it at the beginning. It includes a C program that implements this algorithm and demonstrates how to input an array, sort it, and print the sorted list. The time complexity of the selection sort is O(n^2).

Uploaded by

Hetvi Chauhan
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)
23 views2 pages

Selection Sort

The document describes the selection sort algorithm, which sorts an array by repeatedly finding the minimum element from the unsorted part and placing it at the beginning. It includes a C program that implements this algorithm and demonstrates how to input an array, sort it, and print the sorted list. The time complexity of the selection sort is O(n^2).

Uploaded by

Hetvi Chauhan
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

The selection sort algorithm sorts an array by repeatedly finding the minimum element

(considering ascending order) from unsorted part and putting it at the beginning.

Example:

Program:

1. #include <stdio.h>
2.  
3. int main()
4. {
5.   int array[100], n, c, d, position, swap;
6.  
7.   printf("Enter number of elements\n");
8.   scanf("%d", &n);
9.  
10.   printf("Enter %d integers\n", n);
11.  
12.   for (c = 0; c < n; c++)
13.     scanf("%d", &array[c]);
14.  
15.   for (c = 0; c < (n - 1); c++)
16.   {
17.     position = c;
18.    
19.     for (d = c + 1; d < n; d++)
20.     {
21.       if (array[position] > array[d])
22.         position = d;
23.     }
24.     if (position != c)
25.     {
26.       swap = array[c];
27.       array[c] = array[position];
28.       array[position] = swap;
29.     }
30.   }
31.  
32.   printf("Sorted list in ascending order:\n");
33.  
34.   for (c = 0; c < n; c++)
35.     printf("%d\n", array[c]);
36.  
37.   return 0;
38. }

Complexity

n+(n-1)+(n-2)+...+1 =n(n+1)/2

So, Complexity is O(n2)

You might also like