GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 27-Oct-2009, 16:45
jnCPP jnCPP is offline
New Member
 
Join Date: Oct 2009
Posts: 9
jnCPP will become famous soon enough

Help: selection sort algorithm -- alphabetical


Below is the code I've written to sort each separate goals/assists/shots array in ascending order, the problem I'm having is that I don't know how to sort the string of names in alphabetical order using the selection sort algorithm.

I'm not asking for a solution, just wondering if I can approach the string array the same as I would an int array, and if not, how I would possibly retrieve the first letter of each string array element and compare it with the rest?

Thanks in advance


CPP / C++ / C Code:
void sortArrays(string players[], int goals[], int assists[], int shots[], int numPlayers)
{
int top = 0,
    last = numPlayers,
	ssf,
	ptr,
	temp;
string swap;
	
do
{
ptr = top;
ssf = top;
	do 
	{
	  if (goals[ptr] < goals[ssf])
	  {
	  ssf = ptr;
	  }
  	  ptr = ptr + 1;
	}
	while (ptr < last);
temp = goals[top];
goals[top] = goals[ssf];
goals[ssf] = temp;

;

top = top + 1;
}
while (top < last);


top = 0;
do
{
ptr = top;
ssf = top;
	do 
	{
	  if (shots[ptr] < shots[ssf])
	  {
	  ssf = ptr;
	  }
  	  ptr = ptr + 1;
	}
	while (ptr < last);
temp = shots[top];
shots[top] = shots[ssf];
shots[ssf] = temp;

top = top + 1;
}
while (top < last);

top = 0;
do
{
ptr = top;
ssf = top;
	do 
	{
	  if (assists[ptr] < assists[ssf])
	  {
	  ssf = ptr;
	  }
  	  ptr = ptr + 1;
	}
	while (ptr < last);
temp = assists[top];
assists[top] = assists[ssf];
assists[ssf] = temp;

top = top + 1;
}
while (top < last);
}
Last edited by LuciWiz : 27-Oct-2009 at 16:58. Reason: Please insert your C++ code between [cpp] & [/cpp] tags
  #2  
Old 27-Oct-2009, 18:14
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 803
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: Help: selection sort algorithm -- alphabetical


There are a lot of very useful tools in the string class. cplusplus.com/reference/string/string/
One of them you might be interested in is the [ ] operator.
CPP / C++ / C Code:
#include <iostream>
using namespace std;

int main()
{
    string s1 = "hello";
    cout << "s1= <" << s1 << ">\ns1[1]= " << s1[1] << endl;
    return 0;
}
output
Code:
s1= <hello> s1[1]= e
There are others. Give 'em a look.
  #3  
Old 27-Oct-2009, 19:31
jnCPP jnCPP is offline
New Member
 
Join Date: Oct 2009
Posts: 9
jnCPP will become famous soon enough

Re: Help: selection sort algorithm -- alphabetical


Thanks, but I figured it out,
I just used the same code in my selection sort algorithm for
the integers for the strings, and it worked out just fine.
 
 

Recent GIDBlogAccepted for Ph.D. program by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Selection Sort nurulshidanoni C++ Forum 2 06-May-2008 05:39
Quick Sort Algorithm Peter_APIIT C++ Forum 3 03-Oct-2007 23:45
linked lists selection and insertion sort iceman2006 C++ Forum 1 27-Jun-2006 08:29
Merge Sort Algorithm .c eclipt C Programming Language 1 28-Dec-2005 03:56
Selection Sorting--Help Urgently Needed TetrahedreX C Programming Language 2 03-May-2005 11:57

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 02:39.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.