Selection sort method in Single dimensional array

Selection sort method is one of the must know the sorting algorithm for each and every computer science student.

In this sorting program, we are sorting an integer array of size 10 using bubble sort method.  This method is sorting this array into ascending order.

Selection sorting on a single dimensional array using functions. IN this sorting method first of all we try to find out lowest number available in the list and then swap that position’s value with the value available in that step position.

/* selection sort algorithm
made by :rakesh kumar
*/
#include<iostream>
using namespace std;

void output(int a[],int n){
for(int i=0;i<n;i++)
cout<<setw(10)<<a[i];
}

void input(int x[],int n){
for(int i=0;i<n;i++)
{
cout<<“Enter x[“<<i+1<<“] number : “;
cin>>x[i];
}
}

void selection_sort(int x[] ,int n)
{
int i,j,low,pos,temp;
for(i=0;i<n;i++)
{
low = x[i];
pos = i;
for(j=i+1;j<n;j++){
if(x[j]>low)
{
low = x[j];
pos= j;
}
}
temp = x[i];
x[i]= x[pos];
x[pos]= temp;

}
}

int main()
{
int x[10];
input(x,10);
selection_sort(x,10);
cout<<“\n sorted Array :”;
output(x,10);
return 0;
}

Other most useful sorting algorithm are

  1. Bubble sort method program using single dimensional array
  2. Insertion sort method program using single dimensional array

If you need any other program on any sorting method, do post your request in comments.

Author: rakesh kumar

cbsetoday.com is my online home, here I share all the information related to education, programming - and everything else that attract me. You can get in touch with me in these social sharing websites
Download File :

Leave a Reply