Popular Articles

India is home to great literature and two of the greatest epics originated from the sub-continent. They are the Ramayana and the Mahabharata. The Mahabharata
Gift buying is something most people just swear by. Of all things it counts up to make a gift count, it can become a nightmare for some to zero in on the
Till the sixties of the last century Abu Dhabi was a barren piece of land on the edge of the great Arabian Desert. The discovery of oil changed all that

Latest Articles

Till the sixties of the last century Abu Dhabi was a barren piece of land on the edge of the great Arabian Desert. The discovery of oil changed all that
Just a month ago Google has launched the latest version Android 5.0 called lollipop. Right now not all android devices will get the update for this latest
Rape is nothing new and is one of the oldest crimes known to man. It stems from a desire for sexual intercourse with a prized girl or woman or simply the

/* Include required header files */

#include<iostream.h>
#include<stdio.h>
#include<string.h>
#include<conio.h>

Selection sort method. This method accepts pointer to the character array which contain the characters to be sorted
and the length of the array.

void selsort(char *items,int count){

// Local variable declaration
int a,b,c;
int exchange;
char t;

// Loop over the array of characters
for(a
=0;a<count-1;++a){

exchange=0;
c=a;
t=items[a];


Test and swap the current character with the next position till the next character is less than the current one. In a way to say move the character to its position in the present iteration of sorting. The continue with the next character and so on... For example, consider the word "sortme" In the first iteration the character 's' will be moved to the third position by comparing with each character until the character 't' founds, which is greater than 's'.

for(b=a+1;b<count;++b){
if(items[b]<t){
c=b;
t=items[b];
exchange=1;
}
}
if(exchange){
items[c]=items[a];
items[a]=t;
}
}
}

Now use the above methods to sort the characters of a string.

void main(){
clrscr();
char s[30];
cout<<"Enter A String:";
gets(s);
selsort(s,strlen(s));
cout<<"The String After Selection Sort:"<<s;
getch();
}

  • No comments found