0% found this document useful (0 votes)
10 views5 pages

Algo Assignment-03 Selection Sort

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

Algo Assignment-03 Selection Sort

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

ALGORITHM PRACTICAL ASSIGNMENTS (SOLUTIONS)

GROUP-B
(Sorting: Quadratic & logarithmic time complexities)

Assign- 1) Implement Selection Sort (The program should report the number of
pass & comparisons)
PROCEDURE

In selection sort, the smallest value among the unsorted elements of the array is
selected in every pass and inserted to its appropriate position into the array. It is also
the simplest algorithm. It is an in-place comparison sorting algorithm. In this
algorithm, the array is divided into two parts, first is sorted part, and another one is
the unsorted part. Initially, the sorted part of the array is empty, and unsorted part is
the given array. Sorted part is placed at the left, while the unsorted part is placed at
the right.
In selection sort, the first smallest element is selected from the unsorted array and
placed at the first position. After that second smallest element is selected and placed
in the second position. The process continues until the array is entirely sorted.
The average and worst-case complexity of selection sort is O(n2), where n is the
number of items. Due to this, it is not suitable for large data sets.

ALGORITHM

SELECTION SORT(ARR, N)
Step 1: Repeat Steps 2 and 3 for K = 1 to N-1
Step 2: CALL SMALLEST(ARR, K, N, POS)
Step 3: SWAP A[K] with ARR[POS]
[END OF LOOP]
Step 4: EXIT

SMALLEST (ARR, K, N, POS)


Step 1: [INITIALIZE] SET SMALL = ARR[K]
Step 2: [INITIALIZE] SET POS = K
Step 3: Repeat for J = K+1 to N -1
IF SMALL > ARR[J]
SET SMALL = ARR[J]
SET POS = J
[END OF IF]
[END OF LOOP]
Step 4: RETURN POS

S. P. Dua (Computer Science, Midnapore College, 7872361926, www.gvtcs.com) 1


PROGRAM

/* Selection Sort */
#include<stdio.h>
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>

int Smallest(int[],int,int);
int com=0, tcom=0;

void main()
{
clrscr();
FILE *in;
int n, i, Item, a[100];
cout<<"Enter Sample Size (No of Elements):";
cin>>n;

in=fopen("Algo\\Input.txt", "r");
i=0;
while(!feof(in))
fscanf(in, "%d", &a[i++]);
fclose(in);

FILE *out;
out=fopen("Algo\\Output.txt", "a+");

for(i=0;i<n;i++)
{
int pos = Smallest(a,n,i);
int temp = a[i];
a[i]=a[pos];
a[pos] = temp;
fprintf(out, "Pass:%d Comarison:%d", i+1, com);
tcom+=com;
}

fprintf(out, "\nTotal Pass:%d Total Comarison:%d", n, tcom);


fprintf(out, "\nSorted List:\n");
for(i=0;i<n;i++)
fprintf(out, "%d ", a[i]);
fclose(out);
printf("Done");

getch();
}

S. P. Dua (Computer Science, Midnapore College, 7872361926, www.gvtcs.com) 2


int Smallest(int a[], int n, int i)
{
int small, pos, j;
small = a[i];
pos = i; com=0;
for(j=i+1;j<n;j++)
{
if(a[j]<small)
{
small = a[j];
pos=j; com++;
}
}
return pos;
}

OUTPUT

Do yourself

DISCUSSION

To understand the working of the Selection sort algorithm, let's take an


unsorted array. It will be easier to understand the Selection sort via an
example.

Let the elements of array are -

Now, for the first position in the sorted array, the entire array is to be scanned
sequentially.

At present, 12 is stored at the first position, after searching the entire array, it
is found that 8 is the smallest value.

So, swap 12 with 8. After the first iteration, 8 will appear at the first position in
the sorted array.

For the second position, where 29 is stored presently, we again sequentially


scan the rest of the items of unsorted array. After scanning, we find that 12 is
the second lowest element in the array that should be appeared at second
position.
S. P. Dua (Computer Science, Midnapore College, 7872361926, www.gvtcs.com) 3
Now, swap 29 with 12. After the second iteration, 12 will appear at the second
position in the sorted array. So, after two iterations, the two smallest values
are placed at the beginning in a sorted way.

The same process is applied to the rest of the array elements. Now, we are
showing a pictorial representation of the entire sorting process.

Now, the array is completely sorted.

TIME & SPACE COMPLEXITY

Time Complexity
Case Time Complexity
Best Case: O(n2)
Average Case: O(n2)
Worst Case: O(n2)
Best Case Complexity - It occurs when there is no sorting required, i.e. the
array is already sorted. Then best-case time complexity sort is O(n2).

S. P. Dua (Computer Science, Midnapore College, 7872361926, www.gvtcs.com) 4


Average Case Complexity - It occurs when the array elements are in jumbled
order that is not properly ascending and not properly descending. The average
case time complexity of selection sort is O(n2).
Worst Case Complexity - It occurs when the array elements are required to
be sorted in reverse order. That means suppose you have to sort the array
elements in ascending order, but its elements are in descending order. The
worst-case time complexity of selection sort is O(n2).

Space Complexity: O(1)

Literature Review

Advantages of Selection Sort Algorithm


• Simple and easy to understand.
• Works well with small datasets.

Disadvantages of the Selection Sort Algorithm


• Selection sort has a time complexity of O(n^2) in the worst and average
case.
• Does not work well on large datasets.
• Does not preserve the relative order of items with equal keys which means it
is not stable.

S. P. Dua (Computer Science, Midnapore College, 7872361926, www.gvtcs.com) 5

You might also like