# Sorting Strings with int Sort: Explained + Tips

• Lancelot59
In summary, the conversation discusses the use of selection sort algorithm to sort arrays, and the process of finding the smallest value in the unsorted list and placing it in the sorted list. The algorithm can be applied to any data type as long as it is comparable. The conversation also mentions a screenshot of a question from the assignment and the completion of a project for an inventory/sales system for a book store.
Lancelot59
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.

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
5.4 KB · Views: 396
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.

## 1. How does sorting strings with int Sort work?

The int Sort method sorts strings by converting them to a list of integers based on their character values. Then, the list is sorted using the built-in sorting function, and the sorted list is converted back to strings.

## 2. What is the advantage of using int Sort for sorting strings?

Using int Sort allows for more accurate sorting of strings, as it takes into account the character values instead of just the alphabetical order. This can be useful in cases where strings have numbers or special characters.

## 3. Can int Sort be used for sorting strings in any language?

Yes, int Sort can be used for sorting strings in any language as it relies on the character values and not specific language rules. However, the results may vary if the language uses non-ASCII characters.

## 4. Are there any tips for optimizing the sorting process with int Sort?

One tip is to use the built-in sorting function that comes with int Sort, as it is optimized for efficiency. Additionally, it may be helpful to convert the strings to lowercase before sorting to ensure consistent results.

## 5. Can int Sort handle strings with different lengths?

Yes, int Sort can handle strings with different lengths as it sorts based on the character values and not the length of the strings. However, it is important to note that the results may not be intuitive if the strings have different lengths.

• Engineering and Comp Sci Homework Help
Replies
5
Views
3K
• Engineering and Comp Sci Homework Help
Replies
3
Views
887
• Engineering and Comp Sci Homework Help
Replies
7
Views
2K
• Engineering and Comp Sci Homework Help
Replies
5
Views
2K
• Engineering and Comp Sci Homework Help
Replies
1
Views
6K
• Engineering and Comp Sci Homework Help
Replies
21
Views
2K
• Engineering and Comp Sci Homework Help
Replies
4
Views
1K
• Engineering and Comp Sci Homework Help
Replies
18
Views
2K
• Engineering and Comp Sci Homework Help
Replies
3
Views
1K
• Engineering and Comp Sci Homework Help
Replies
1
Views
2K