Computer Classes

Computer Classes

Share

This page is regarding class's updates to students. We are tacking all computer subjects for BCA,BCS,MCA,MCS.

Photos 04/05/2017

Selection Sort

The selection sort is a combination of searching and sorting. During each pass, the unsorted element with the smallest value is moved to its proper position in the array. The number of times the sort passes through the array is one less than the number of items in the array.

Steps/Procedures

Symbol | in following image divides array into 1.Sorted and 2. unsorted array
Left of | is sorted array and right side is unsorted array

1. Divide array into
a. Sorted array
b. Unsorted Array
2. Take first element from unsorted array and select proper
(smallest) element from unsorted array and swap it with
first element.
3. Every iteration sorted array is increased by one element
4. Again do the procedure till end of array

Program :
// C program for implementation of selection sort


void selectionSort(int arr[], int n)
{
int i, j, min_idx,temp;

// One by one move boundary of unsorted subarray
for (i = 0; i < n-1; i++)
{
// Find the minimum element in unsorted array
min_idx = i;
for (j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;

// Swap the found minimum element with the first element
temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}

/* Function to display an array */
void displayArray(int arr[], int n)
{
int i;
for (i=0; i < n; i++)
printf("%d ", arr[i]);
}
// main function
int main()
{
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr)/sizeof(arr[0]);
selectionSort(arr, n);
printf("Sorted array: \n");
displayArray(arr, n);
return 0;
}

Want your school to be the top-listed School/college in Pune?
Click here to claim your Sponsored Listing.

Category

Telephone

Address


Akurdi Railway Station
Pune
411035