Sorting Strings with int Sort: Explained + Tips

Click For Summary

Discussion Overview

The discussion revolves around adapting a selection sort algorithm designed for integers to sort strings. Participants explore the mechanics of the selection sort algorithm and seek clarification on its application to string data types, as well as share progress on related programming tasks.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Homework-related

Main Points Raised

  • One participant requests an explanation of how the selection sort algorithm works for integers and how it can be adapted for strings.
  • Another participant describes the selection sort process, emphasizing its O(n²) time complexity and outlining the steps involved in finding and placing the lowest value in the sorted list.
  • A participant suggests that the algorithm can be applied to any comparable data type, including strings.
  • One participant mentions a datatype mismatch issue encountered while working on a related assignment, leading to unexpected output.
  • Another participant encourages drawing out the algorithm to better understand it, indicating that visual aids can help clarify the process.
  • A participant confirms successful implementation of the sorting problem with strings and shares details about their ongoing project involving an inventory/sales system.

Areas of Agreement / Disagreement

Participants generally agree on the mechanics of the selection sort algorithm and its applicability to strings, but there is no explicit consensus on the best approach to adapt the algorithm for strings, as some details remain unaddressed.

Contextual Notes

Some participants express uncertainty about specific details of the algorithm's implementation for strings and the implications of datatype mismatches.

Who May Find This Useful

Readers interested in sorting algorithms, programming challenges involving data types, and those working on similar coding assignments may find this discussion beneficial.

Lancelot59
Messages
640
Reaction score
1
Hi there PF. For this problem I need to take the following code which apparently sorts an array with integers, and sort strings instead. However I DO NOT understand how this works for integers, much less how I could make it work for strings. Could someone please explain it to me? A few tips to get me started on the string sorting would be appreciated too.

Code:
void selectionSort(int array[], int size)
 {
    int startScan, minIndex, minValue;
 
    for (startScan = 0; startScan < (size - 1); startScan++)
    {
       minIndex = startScan;
       minValue = array[startScan];
       for(int index = startScan + 1; index < size; index++)
      {
          if (array[index] < minValue)
        {
             minValue = array[index];
             minIndex = index;
        }
     }
       array[minIndex] = array[startScan];
       array[startScan] = minValue;
    }
 }

There are some similar problems I might be popping in with later on, but I figure this is the best place to start.
 
Physics news on Phys.org
SelectionSort is a sorting technique which runs in O(n²) time, meaning that it's inefficient for large lists.

That aside, it works like this:
1. Find the lowest value in the unsorted list
2. Place it at the end of the sorted list
3. Repeat for remainder of the unsorted list

Your outer loop is basically a way to keep track of where the ordered part of the array ends. The variable startScan tells the second loop where to start scanning for the lowest value in the unordered list, and thus is the start of the unordered part of your array.

The second loop finds the smallest value in the unordered part of the array. Once found, which it will never know until it reaches the end, th algorithm swaps with the first unsorted element, which is at the index that startScan contains. If our previous list had n sorted elements, it now has n+1 sorted elements.

Then counter is incremented and the process is repeated.

You can do this with any data type, as long as it's comparable (and strings are comparable in most OO languages!)
 
Alright, it makes a bit more sense now.

This screenshot attached is for a question further down the assignment. Due to a datatype mismatch instead of printing an array it beeped three times and printed this...
 

Attachments

  • funny screenshot.png
    funny screenshot.png
    5.4 KB · Views: 466
If you don't get the algorithm, draw it out on paper. That usually does it.

Here is a drawn out version which explains it pretty well if you follow what I posted.

It seems like it just printed some byte values, but I can't discern much more without some more details.
 
Oh, that screenshot is from the next question down. :p

I managed to get the sorting problem with your help.
 
Good!
Did you get it working with strings, now?
 
Yup, it went perfectly. I'm currently working on my final project. An inventory/sales system for a book store :/

The thing is freaking massive. I just finished coding the inventory section and I'm going to debug it later today.
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 7 ·
Replies
7
Views
3K
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
6K
  • · Replies 21 ·
Replies
21
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 18 ·
Replies
18
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K