Sie sind auf Seite 1von 6

ASSIGNMENT 18

SORTING OF AN UNSORTED ARRAY BY USING SELECTION SORT

STATEMENT:

The ‘Selection Sort’ requires that all keys under sorting be available prior to the execution.
Further, the selection sort considers the following two basic operations.

i)Select: selection of an item from the input list.

ii)Swap: Interchange two items in the list. In selection sorting the above two steps are
iterated to produce the sorted list, which progresses from one end as the iteration
continues. The selection sort can be done on the input list itself. This implies that selection
sort is an in place sorting technique.

PICTORIAL REPRESENTATION OF SELECTION SORT TECHNIQUE:


ALGORITHM:

INPUT: An unsorted array a[6,2,4,1,…………….,n]

OUTPUT: An sorted array a[1,2,3,4,5,…………..,n] (in ascending order)

PROCESS:
Step 1: Create a function “void selectionsort(int a[],int n)”

Step 1.1: For i=0 to i<n repeat Step 1.2 to Step 1.8

Step 1.2: Set mina[i]

Step 1.3: Set posi

Step 1.4: For j=i+1 to j<n repeat Step 1.5

Step 1.5: If(a[j]<min) then

Set mina[j] and Set posj

[End of Step 1.4 ‘For’ loop]


Step 1.6: Set ta[i]

Step 1.7: Set a[i] a[pos]

Step1.8: Set a[pos] t

[End of Step 1.1 ‘For’ loop]

[End of the function “void selectionsort(int a[],int n)”]

Step 2: Create the function “void main()”


Step 2.1 Take the number of elements from the user in an integer type variable n

Step 2.2: Take the unsorted elements from the user in an array ‘a’

Step 2.3: Print the unsorted array ‘a’

Step 2.4: Call the function ‘selectionsort(a,n)’

Step 2.5: Print the sorted elements

[End of the function “void main()”]

Step 3: Stop.

PROGRAM CODE:

#include<stdio.h>

#include<conio.h>

//*Creation of the function 'selectionsort()' to sort an unsorted array using selection sort
technique*//

void selectionsort(int a[],int n)

int i,j,t,min,pos;

//*Sorting of the unsorted array*//

for(i=0;i<n;i++)

min=a[i];

pos=i;
for(j=i+1;j<n;j++)

if(a[j]<min)

min=a[j];

pos=j;

t=a[i];

a[i]=a[pos];

a[pos]=t;

//*Creating the ‘main’ function*//

void main()

int n,a[20],i;

//*Taking the number of elements of the unsorted array*//

printf("Enter the number of elements: ");

scanf("%d",&n);

//*Taking the unsorted elements from the user in array 'a'*//

for(i=0;i<n;i++)

printf("Enter a[%d]th element: ",i);

scanf("%d",&a[i]);

}
//* Printing the unsorted elements of the array 'a'*//

printf("\n\n the unsorted array is..........\n\n\t");

for(i=0;i<n;i++)

printf("%d ",a[i]);

//*Calling the function 'selectionsort()'*//

selectionsort(a,n);

//*Print the sorted elements of the array 'a'*//

printf("\n\nthe sorted array is..........\n\n\t");

for(i=0;i<n;i++)

printf("%d ",a[i]);

getch();

PROGRAM OUTPUT:

DISCUSSION:
The selection sort performs the sorting on the same list as the input list. Hence, it is an in
place sorting technique. In other words, the straight selection sort does not require
additional storage space other than space to store the input list. The best case, average case
and worst case complexity of the selection sort is O(n2) . Among simple average-case O(n2)
algorithms, selection sort almost always outperforms bubble sort. Insertion sort is very similar in
that after the kth iteration, the first k elements in the array are in sorted order. Insertion sort's
advantage is that it only scans as many elements as it needs in order to place the k + 1st element,
while selection sort must scan all remaining elements to find the k + 1st element.Simple
calculation shows that insertion sort will therefore usually perform about half as many
comparisons as selection sort, although it can perform just as many or far fewer depending on the
order the array was in prior to sorting. It can be seen as an advantage for some real-time
applications that selection sort will perform identically regardless of the order of the array, while
insertion sort's running time can vary considerably. However, this is more often an advantage for
insertion sort in that it runs much more efficiently if the array is already sorted or "close to
sorted." While selection sort is preferable to insertion sort in terms of number of writes (O(n)
swaps versus Ο(n2) swaps), it almost always far exceeds (and never beats) the number of writes
that cycle sort makes, as cycle sort is theoretically optimal in the number of writes. This can be
important if writes are significantly more expensive than reads, such as with EEPROM or Flash
memory, where every write lessens the lifespan of the memory.

Finally, selection sort is greatly outperformed on larger arrays by O(n log n) divide-and-conquer
algorithms such as mergesort. However, insertion sort or selection sort are both typically faster
for small arrays (i.e. fewer than 10–20 elements). A useful optimization in practice for the
recursive algorithms is to switch to insertion sort or selection sort for "small enough" sublists.

Das könnte Ihnen auch gefallen