0% found this document useful (0 votes)
6 views3 pages

Select Sort Trace

The document provides a C program that implements the selection sort algorithm to sort an array of integers. It includes user input for the number of elements and the elements themselves, followed by a detailed step-by-step tracing of the sorting process using a sample array. The final output is the sorted array displayed to the user.

Uploaded by

nikita.004967
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)
6 views3 pages

Select Sort Trace

The document provides a C program that implements the selection sort algorithm to sort an array of integers. It includes user input for the number of elements and the elements themselves, followed by a detailed step-by-step tracing of the sorting process using a sample array. The final output is the sorted array displayed to the user.

Uploaded by

nikita.004967
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/ 3

#include <stdio.

h>
void selection_sort(int array[], int length);
int main()
{
int array[100], length, i;

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


scanf("%d", &length);

printf("Enter %d elements: ", length);


for (i = 0; i < length; i++) {
scanf("%d", &array[i]);
}

selection_sort(array, length);

printf("The sorted array is: ");


for (i = 0; i < length; i++) {
printf("%d ", array[i]);
}

printf("\n");

return 0;

void selection_sort(int array[], int length)


{
int i, j, minIndex, temp;

for (i = 0; i < length - 1; i++) {


minIndex = i;

for (j = i + 1; j < length; j++) {


if (array[j] < array[minIndex]) {
minIndex = j;
}
}

temp = array[i];
array[i] = array[minIndex];
array[minIndex] = temp;
}
return;
}

Let's trace the Selection Sort step by step for the array:

Step-by-Step Tracing

Initial Array

[30, 10, 12, 8, 15, 1]

Pass 1 (i = 0, minIndex = 0)

Find the minimum element in [30, 10, 12, 8, 15, 1]

• Compare 30 with 10 → minIndex = 1

• Compare 10 with 12 → minIndex = 1

• Compare 10 with 8 → minIndex = 3

• Compare 8 with 15 → minIndex = 3

• Compare 8 with 1 → minIndex = 5

Swap array[0] and array[5]


New Array: [1, 10, 12, 8, 15, 30]

Pass 2 (i = 1, minIndex = 1)

Find the minimum element in [10, 12, 8, 15, 30]

• Compare 10 with 12 → minIndex = 1

• Compare 10 with 8 → minIndex = 3

• Compare 8 with 15 → minIndex = 3

• Compare 8 with 30 → minIndex = 3

Swap array[1] and array[3]


New Array: [1, 8, 12, 10, 15, 30]
Pass 3 (i = 2, minIndex = 2)

Find the minimum element in [12, 10, 15, 30]

• Compare 12 with 10 → minIndex = 3

• Compare 10 with 15 → minIndex = 3

• Compare 10 with 30 → minIndex = 3

Swap array[2] and array[3]


New Array: [1, 8, 10, 12, 15, 30]

Pass 4 (i = 3, minIndex = 3)

Find the minimum element in [12, 15, 30]

• Compare 12 with 15 → minIndex = 3

• Compare 12 with 30 → minIndex = 3

No swap needed.
Array remains: [1, 8, 10, 12, 15, 30]

Pass 5 (i = 4, minIndex = 4)

Find the minimum element in [15, 30]

• Compare 15 with 30 → minIndex = 4

No swap needed.
Array remains: [1, 8, 10, 12, 15, 30]

Final Sorted Array:

[1, 8, 10, 12, 15, 30]

You might also like