Selection Sort of the String

Hi

I need to do a modular coding to programme the function that does a [b]selection sort in ascending order for strings[/b].

User inputs 16 strings in the function main.
That to be passed to the following function;

[b]void sortSelectionString(char* stringArray[], int size)[/b]
(*The type of the function and the parameter is given and can't change)

I coded as followes but, it doesn't sort at all!!
For example, if I input (to the scanf in main)

Ccc
Abc
abc
Bcc

[b]Output[/b] should be...

abc
Abc
Bcc
Ccc

(*following the rule of the functiom srtcmp)

But, my result is... it just prints out as is.... (meaning my selection sort coding is not working at all!!)

Could anyone advise..?

[code]
/* Selection Sort in Ascending Order */
void sortSelectionString(char* stringArray[], int size) {

int unsortedEnd; //The end index of unsorted part
int i; //Counter for-loop
int largestIndex; //Keep the track (a counter) of the largest element
int temp;

//Examine from the last element in the array
for(unsortedEnd = size - 1; unsortedEnd > 0; unsortedEnd--){

//Search for the largest value of the string in unsorted part
largestIndex = 0;
for(i=1; i<=unsortedEnd; i++){
if(stringArray[largestIndex] < stringArray[i]) { //If the one-next element is larger
largestIndex = i; //Assing it as a largest element in unsorted
}/*end if*/
}/*end inner for-loop*/

//Swap with the end of unsorted
temp = stringArray[unsortedEnd];
stringArray[unsortedEnd] = stringArray[largestIndex];
stringArray[largestIndex] = temp; //Assing unsortedEnd as the largest of unsorted part
}/*end outer for-loop*/

}/*end function sortSelectionString*/
[/code]

Thanks!! The above coding (logic) works to do selection-sort on numeric (int) values.. but not for the string.

[b][red]Do I need to examine one character by character IN THE STRING ALSO.. maybe?[/red][/b]

Comments

  • : Hi
    :
    : I need to do a modular coding to programme the function that does a [b]selection sort in ascending order for strings[/b].
    :
    : User inputs 16 strings in the function main.
    : That to be passed to the following function;
    :
    : [b]void sortSelectionString(char* stringArray[], int size)[/b]
    : (*The type of the function and the parameter is given and can't change)
    :
    : I coded as followes but, it doesn't sort at all!!
    : For example, if I input (to the scanf in main)
    :
    : Ccc
    : Abc
    : abc
    : Bcc
    :
    : [b]Output[/b] should be...
    :
    : abc
    : Abc
    : Bcc
    : Ccc
    :
    : (*following the rule of the functiom srtcmp)
    :
    : But, my result is... it just prints out as is.... (meaning my selection sort coding is not working at all!!)
    :
    : Could anyone advise..?
    :
    : [code]
    : /* Selection Sort in Ascending Order */
    : void sortSelectionString(char* stringArray[], int size) {
    :
    : int unsortedEnd; //The end index of unsorted part
    : int i; //Counter for-loop
    : int largestIndex; //Keep the track (a counter) of the largest element
    : int temp;
    :
    : //Examine from the last element in the array
    : for(unsortedEnd = size - 1; unsortedEnd > 0; unsortedEnd--){
    :
    : //Search for the largest value of the string in unsorted part
    : largestIndex = 0;
    : for(i=1; i<=unsortedEnd; i++){
    : if(stringArray[largestIndex] < stringArray[i]) { //If the one-next element is larger
    : largestIndex = i; //Assing it as a largest element in unsorted
    : }/*end if*/
    : }/*end inner for-loop*/
    :
    : //Swap with the end of unsorted
    : temp = stringArray[unsortedEnd];
    : stringArray[unsortedEnd] = stringArray[largestIndex];
    : stringArray[largestIndex] = temp; //Assing unsortedEnd as the largest of unsorted part
    : }/*end outer for-loop*/
    :
    : }/*end function sortSelectionString*/
    : [/code]
    :
    : Thanks!! The above coding (logic) works to do selection-sort on numeric (int) values.. but not for the string.
    :
    : [b][red]Do I need to examine one character by character IN THE STRING ALSO.. maybe?[/red][/b]
    :


    temp = stringArray[unsortedEnd];

    You assign a char pointer to an int, shouldn't work without typecast.
    If that codes compiles without errors, you *really* need a new compiler.
  • : : Hi
    : :
    : : I need to do a modular coding to programme the function that does a [b]selection sort in ascending order for strings[/b].
    : :
    : : User inputs 16 strings in the function main.
    : : That to be passed to the following function;
    : :
    : : [b]void sortSelectionString(char* stringArray[], int size)[/b]
    : : (*The type of the function and the parameter is given and can't change)
    : :
    : : I coded as followes but, it doesn't sort at all!!
    : : For example, if I input (to the scanf in main)
    : :
    : : Ccc
    : : Abc
    : : abc
    : : Bcc
    : :
    : : [b]Output[/b] should be...
    : :
    : : abc
    : : Abc
    : : Bcc
    : : Ccc
    : :
    : : (*following the rule of the functiom srtcmp)
    : :
    : : But, my result is... it just prints out as is.... (meaning my selection sort coding is not working at all!!)
    : :
    : : Could anyone advise..?
    : :

    [purple]
    : temp = stringArray[unsortedEnd];
    :
    : You assign a char pointer to an int, shouldn't work without typecast.
    : If that codes compiles without errors, you *really* need a new compiler.
    :
    [/purple]

    Hi Lundin

    Right!! I didn't pay attention to that..
    Change the type of [b]temp[/b] to char, then made it to a pointer.
    Now it executes but,, it doesn't do a selection sort.
    So I think somewhere in the logic is being wrong...

    But why it's not working...?

    This is the modified code;
    [code]
    #include
    #include
    #include

    void printArray(char* stringArray[], int size);

    /* Selection Sort in Ascending Order */
    void sortSelectionString(char* stringArray[], int size) {

    int unsortedEnd; //The end index of unsorted part
    int i; //Counter for-loop
    int largestIndex; //The index to keep the track (a counter) of the largest element
    char* temp; //Store char, used for swapping

    //Examine from the last element in the array
    for(unsortedEnd = size - 1; unsortedEnd > 0; unsortedEnd--){

    //Search for the largest value of the string in unsorted part
    largestIndex = 0;
    for(i=1; i<=unsortedEnd; i++){
    if(stringArray[largestIndex] < stringArray[i]) { //If the one-next element is larger
    largestIndex = i; //Assing it as a largest element in unsorted
    }/*end if*/
    }/*end inner for-loop*/

    //Swap with the end of unsorted
    temp = stringArray[unsortedEnd];
    stringArray[unsortedEnd] = stringArray[largestIndex];
    stringArray[largestIndex] = temp; //Assing unsortedEnd as the largest of unsorted part
    }/*end outer for-loop*/

    }/*end function sortSelectionString*/
    [/code]
  • : : : Hi
    : : :
    : : : I need to do a modular coding to programme the function that does a [b]selection sort in ascending order for strings[/b].
    : : :
    : : : User inputs 16 strings in the function main.
    : : : That to be passed to the following function;
    : : :
    : : : [b]void sortSelectionString(char* stringArray[], int size)[/b]
    : : : (*The type of the function and the parameter is given and can't change)
    : : :
    : : : I coded as followes but, it doesn't sort at all!!
    : : : For example, if I input (to the scanf in main)
    : : :
    : : : Ccc
    : : : Abc
    : : : abc
    : : : Bcc
    : : :
    : : : [b]Output[/b] should be...
    : : :
    : : : abc
    : : : Abc
    : : : Bcc
    : : : Ccc
    : : :
    : : : (*following the rule of the functiom srtcmp)
    : : :
    : : : But, my result is... it just prints out as is.... (meaning my selection sort coding is not working at all!!)
    : : :
    : : : Could anyone advise..?
    : : :
    :
    : [purple]
    : : temp = stringArray[unsortedEnd];
    : :
    : : You assign a char pointer to an int, shouldn't work without typecast.
    : : If that codes compiles without errors, you *really* need a new compiler.
    : :
    : [/purple]
    :
    : Hi Lundin
    :
    : Right!! I didn't pay attention to that..
    : Change the type of [b]temp[/b] to char, then made it to a pointer.
    : Now it executes but,, it doesn't do a selection sort.
    : So I think somewhere in the logic is being wrong...
    :
    : But why it's not working...?
    :
    : This is the modified code;
    : [code]
    : #include
    : #include
    : #include
    :
    : void printArray(char* stringArray[], int size);
    :
    : /* Selection Sort in Ascending Order */
    : void sortSelectionString(char* stringArray[], int size) {
    :
    : int unsortedEnd; //The end index of unsorted part
    : int i; //Counter for-loop
    : int largestIndex; //The index to keep the track (a counter) of the largest element
    : char* temp; //Store char, used for swapping
    :
    : //Examine from the last element in the array
    : for(unsortedEnd = size - 1; unsortedEnd > 0; unsortedEnd--){
    :
    : //Search for the largest value of the string in unsorted part
    : largestIndex = 0;
    : for(i=1; i<=unsortedEnd; i++){
    : if(stringArray[largestIndex] < stringArray[i]) { //If the one-next element is larger
    [red]/* Here you are comparing the [b]address[/b] of two strings, not the contents.
    Use strcmp() instead */[/red]
    : largestIndex = i; //Assing it as a largest element in unsorted
    : }/*end if*/
    : }/*end inner for-loop*/
    :
    : //Swap with the end of unsorted
    : temp = stringArray[unsortedEnd];
    : stringArray[unsortedEnd] = stringArray[largestIndex];
    : stringArray[largestIndex] = temp; //Assing unsortedEnd as the largest of unsorted part
    : }/*end outer for-loop*/
    :
    : }/*end function sortSelectionString*/
    : [/code]
    :


  • : : This is the modified code;
    : : [code]
    : : #include
    : : #include
    : : #include
    : :
    : : void printArray(char* stringArray[], int size);
    : :
    : : /* Selection Sort in Ascending Order */
    : : void sortSelectionString(char* stringArray[], int size) {
    : :
    : : int unsortedEnd; //The end index of unsorted part
    : : int i; //Counter for-loop
    : : int largestIndex; //The index to keep the track (a counter) of the largest element
    : : char* temp; //Store char, used for swapping
    : :
    : : //Examine from the last element in the array
    : : for(unsortedEnd = size - 1; unsortedEnd > 0; unsortedEnd--){
    : :
    : : //Search for the largest value of the string in unsorted part
    : : largestIndex = 0;
    : : for(i=1; i<=unsortedEnd; i++){
    : : if(stringArray[largestIndex] < stringArray[i]) { //If the one-next element is larger
    : [red]/* Here you are comparing the [b]address[/b] of two strings, not the contents.
    : Use strcmp() instead */[/red]
    : : largestIndex = i; //Assing it as a largest element in unsorted
    : : }/*end if*/
    : : }/*end inner for-loop*/
    : :
    : : //Swap with the end of unsorted
    : : temp = stringArray[unsortedEnd];
    : : stringArray[unsortedEnd] = stringArray[largestIndex];
    : : stringArray[largestIndex] = temp; //Assing unsortedEnd as the largest of unsorted part
    : : }/*end outer for-loop*/
    : :
    : : }/*end function sortSelectionString*/
    : : [/code]
    : :
    :
    :

    Hi

    OK... I didn't think I was comparing the location / address of the element. Changed [b]if[/b] logic by using strcmp(s1, s2). It does execute but still doesn't do sorting.

    [code]
    if(strcmp(stringArray[i],stringArray[largestIndex])) { //If the one-next element is larger
    largestIndex = i; //Assing it as a largest element in unsorted
    }/*end if*/
    [/code]

    Will look into it more...



  • :
    : : : This is the modified code;
    : : : [code]
    : : : #include
    : : : #include
    : : : #include
    : : :
    : : : void printArray(char* stringArray[], int size);
    : : :
    : : : /* Selection Sort in Ascending Order */
    : : : void sortSelectionString(char* stringArray[], int size) {
    : : :
    : : : int unsortedEnd; //The end index of unsorted part
    : : : int i; //Counter for-loop
    : : : int largestIndex; //The index to keep the track (a counter) of the largest element
    : : : char* temp; //Store char, used for swapping
    : : :
    : : : //Examine from the last element in the array
    : : : for(unsortedEnd = size - 1; unsortedEnd > 0; unsortedEnd--){
    : : :
    : : : //Search for the largest value of the string in unsorted part
    : : : largestIndex = 0;
    : : : for(i=1; i<=unsortedEnd; i++){
    : : : if(stringArray[largestIndex] < stringArray[i]) { //If the one-next element is larger
    : : [red]/* Here you are comparing the [b]address[/b] of two strings, not the contents.
    : : Use strcmp() instead */[/red]
    : : : largestIndex = i; //Assing it as a largest element in unsorted
    : : : }/*end if*/
    : : : }/*end inner for-loop*/
    : : :
    : : : //Swap with the end of unsorted
    : : : temp = stringArray[unsortedEnd];
    : : : stringArray[unsortedEnd] = stringArray[largestIndex];
    : : : stringArray[largestIndex] = temp; //Assing unsortedEnd as the largest of unsorted part
    : : : }/*end outer for-loop*/
    : : :
    : : : }/*end function sortSelectionString*/
    : : : [/code]
    : : :
    : :
    : :
    :
    : Hi
    :
    : OK... I didn't think I was comparing the location / address of the element. Changed [b]if[/b] logic by using strcmp(s1, s2). It does execute but still doesn't do sorting.
    :
    : [code]
    : if(strcmp(stringArray[i],stringArray[largestIndex])) { //If the one-next element is larger
    : largestIndex = i; //Assing it as a largest element in unsorted
    : }/*end if*/
    : [/code]
    :
    : Will look into it more...
    :

    strcmp() returns either a negative or a positive value (or zero if the strings are equal). An if-statement is true for any value different from zero. Read about strcmp() in your C book.
  • : :
    : : : : This is the modified code;
    : : : : [code]
    : : : : #include
    : : : : #include
    : : : : #include
    : : : :
    : : : : void printArray(char* stringArray[], int size);
    : : : :
    : : : : /* Selection Sort in Ascending Order */
    : : : : void sortSelectionString(char* stringArray[], int size) {
    : : : :
    : : : : int unsortedEnd; //The end index of unsorted part
    : : : : int i; //Counter for-loop
    : : : : int largestIndex; //The index to keep the track (a counter) of the largest element
    : : : : char* temp; //Store char, used for swapping
    : : : :
    : : : : //Examine from the last element in the array
    : : : : for(unsortedEnd = size - 1; unsortedEnd > 0; unsortedEnd--){
    : : : :
    : : : : //Search for the largest value of the string in unsorted part
    : : : : largestIndex = 0;
    : : : : for(i=1; i<=unsortedEnd; i++){
    : : : : if(stringArray[largestIndex] < stringArray[i]) { //If the one-next element is larger
    : : : [red]/* Here you are comparing the [b]address[/b] of two strings, not the contents.
    : : : Use strcmp() instead */[/red]
    : : : : largestIndex = i; //Assing it as a largest element in unsorted
    : : : : }/*end if*/
    : : : : }/*end inner for-loop*/
    : : : :
    : : : : //Swap with the end of unsorted
    : : : : temp = stringArray[unsortedEnd];
    : : : : stringArray[unsortedEnd] = stringArray[largestIndex];
    : : : : stringArray[largestIndex] = temp; //Assing unsortedEnd as the largest of unsorted part
    : : : : }/*end outer for-loop*/
    : : : :
    : : : : }/*end function sortSelectionString*/
    : : : : [/code]
    : : : :
    : : :
    : : :
    : :
    : : Hi
    : :
    : : OK... I didn't think I was comparing the location / address of the element. Changed [b]if[/b] logic by using strcmp(s1, s2). It does execute but still doesn't do sorting.
    : :
    : : [code]
    : : if(strcmp(stringArray[i],stringArray[largestIndex])) { //If the one-next element is larger
    : : largestIndex = i; //Assing it as a largest element in unsorted
    : : }/*end if*/
    : : [/code]
    : :
    : : Will look into it more...
    : :
    :
    : strcmp() returns either a negative or a positive value (or zero if the strings are equal). An if-statement is true for any value different from zero. Read about strcmp() in your C book.
    :


    That's right. So I guess I have to add again for if the if-statement produced the positive numbers, execute the following code.

    Thanks!!
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion