Diploma Second year Computer Engineering (Affiliated by MSBTE(Maharashtra State Board

advertisement
Diploma Second year Computer Engineering (Affiliated by MSBTE(Maharashtra State Board
of Technical Education) Mumbai.) question paper of "Data Structure Using C" has following
question:
Write a program in C language for selection sort and arrange the given numbers in
ascending order using selection sort. The numbers are as follows.
16 , 23, 13, 9 ,7 , 5. (This question is asked for 8 Marks.)
Awaiting for quality response from experts.
Simplest type of sorting algorithm is the selection sorting. The working method of selection sort is first
find the smallest element from the list that we enter and exchange it with the value in the first position
in the array. Then next find out the next smallest element from the remaining list and exchange that
element with the value in the second position. This process of finding the smallest element and
exchanging the element with the value in the appropriate position will continue until the array ends.
For example,
List that we enter for performing the selection sort are:
8
15
11
7
9
3
In this array the first process is to find the smallest element. Here the smallest element is 3. Then next
step is to exchange it with the first position. So the element exchanged with the value of first position in
the array, which is 8. Hence output array is:
First step:
3
15
11
7
9
8
Remaining elements does not have any change in their position in the first step. Similarly next find the
second smallest element from the remaining list and exchange it with the value in the second position.
That means, next smallest element 7 is exchanged with 15 in the second position. Then the resulting
array is listed below.
Second step:
3
7
11
15
9
8
Third step:
3
7
8
15
9
11
Fourth step:
3
7
8
9
15
11
3
7
8
9
11
15
Similarly;
Finally;
Fifth step:
So we get a sorted array.
The following are the program for selection sort.
void main()
{
int count,x,y;
int num_array[20];
printf(“Enter the limit or count of the numbers for sorting\n”);
scanf(“%d”,&count);
printf(“Enter the elements for performing the selection sort\n”);
for(x=0;x< count;x++)
{
int t;
for(y=x+1;y< count;y++)
{
if(num_array[x]>num_array[y])
{
t= num_array [x];
num_array [x]= num_array [y];
num_array [y]=t;
}
}
}
printf(“Elements after selection sorting are:”);
for(x=0;x< count;x++)
{
printf(“%d”, num_array [x]);
}
getch();
}
The output for the program is explained below
Enter the limit or count of the numbers for sorting
7
Enter the elements for performing the selection sort
12
9
5
16
4
7
10
Elements after selection sorting are:
4
5
7
9
10
12
16
The processing steps for the above output are explained below:
Input array:
12
9
5
16
4
7
10
First step:
4
9
5
16
12
7
10
Second step:
4
5
9
16
12
7
10
Third step:
4
5
7
16
12
9
10
Fourth step:
4
5
7
9
12
16
10
Fifth step:
4
5
7
9
10
16
12
Sixth step:
4
5
7
9
10
12
16
Hence the sorted lists are getting at the sixth step of process.
Download