Q. Write a program to sort an array elements in ascending order using Selection Sort.
Selection Sort :- The selection Sort is basically the selection of the position of the element from the beginning with the other elements. Elements are compared and exchanged based on position and then the selection position is moved to the next position until it reaches the end.
Ascending Order :- Numbers are said to be in ascending order when they are arranged from smallest to largest number. Such as 5, 9, 13, 17 and 21 are arranged in ascending order.
Selection Sort Algorithm
START Step 1 : Set MIN to location 0 Step 2 : Search the minimum element in the list Step 3 : Swap with value at location MIN Step 4 : Increment MIN to point to next element Step 5 : Repeat until list is sorted STOP
Selection Sort Program
#include <stdio.h> int main(){ int i, j, temp, arr[5]={6,7,2,1,4}; // Logic of selection sort algorithm for(i=0;i<5;i++){ for(j=i+1;j<5;j++){ if(arr[i]>arr[j]){ temp=arr[i]; arr[i]=arr[j]; arr[j]=temp; } } } printf("Sorted elements: "); for(i=0;i<5;i++) printf(" %d",arr[i]); return 0; }
// Selection sort in C++ #include <iostream> using namespace std; void selection_Sort(int arr[], int size) { int temp; for(int i=0;i< size;i++){ for(int j=i+1;j< size;j++){ if(arr[i]>arr[j]){ temp=arr[i]; arr[i]=arr[j]; arr[j]=temp; } } } } void print_Array(int arr[], int size) { for (int i = 0; i < size; ++i) { cout << " " << arr[i]; } cout << "\n"; } int main() { int number[] = {6, 7, 2, 1, 4}; int size = sizeof(number) / sizeof(number[0]); selection_Sort(number, size); cout << "Sorted Array :\n"; print_Array(number, size); }
// Selection sort in Java public class LFC { public static void main(String args[]){ int arr[] = {6, 7, 2, 1, 4}; int size = arr.length; for (int i = 0 ;i< size-1; i++){ int min = i; for (int j = i+1; j< size; j++){ if (arr[j] < arr[min]){ min = j; } } int temp = arr[min]; arr[min] = arr[i]; arr[i] = temp; } for (int i = 0 ;i< size; i++){ System.out.print(" "+arr[i]); } } }
A = [ 6, 7, 2, 1, 4 ]; for i in range(len(A)): min_= i for j in range(i+1, len(A)): if A[min_] > A[j]: min_ = j #swap A[i], A[min_] = A[min_], A[i] # main print(A)
using System; public class LFC { static void Main(string[] args) { int[] arr = new int[5] { 6, 7, 2, 1, 4 }; int n = 5; int temp, smallest; for (int i = 0; i < n - 1; i++) { smallest = i; for (int j = i + 1; j < n; j++) { if (arr[j] < arr[smallest]) { smallest = j; } } temp = arr[smallest]; arr[smallest] = arr[i]; arr[i] = temp; } Console.Write("Sorted array is: "); for (int i = 0; i < n; i++) { Console.Write(arr[i] + " "); } } }
function selection_sort(&$arr, $n) { for($i = 0; $i < $n ; $i++) { $low = $i; for($j = $i + 1; $j < $n ; $j++) { if ($arr[$j] < $arr[$low]) { $low = $j; } } // swap the minimum value to $ith node if ($arr[$i] > $arr[$low]) { $tmp = $arr[$i]; $arr[$i] = $arr[$low]; $arr[$low] = $tmp; } } } // Driver Code $arr = array(6, 7, 2, 1, 4); $len = count($arr); selection_sort($arr, $len); echo "Sorted array : \n"; for ($i = 0; $i < $len; $i++) echo $arr[$i] . " ";
#include <stdio.h> int main(){ int i, j, temp, arr[5]={6,7,2,1,4}; // Logic of selection sort algorithm for(i=0;i<5;i++){ for(j=i+1;j<5;j++){ if(arr[i]>arr[j]){ temp=arr[i]; arr[i]=arr[j]; arr[j]=temp; } } } printf("Sorted elements: "); for(i=0;i<5;i++) printf(" %d",arr[i]); return 0; }
Output
Sorted Array: 1 2 4 6 7
Recommended Programs
Select sort program in descending order.Bubble Sort Program & Algorithm