简体   繁体   English

随机字符串数组的选择排序

[英]Selection Sort of a Array of random strings

I am trying to create a selection sort algorithm that takes in an array of random strings and sorts it.我正在尝试创建一个选择排序算法,该算法接受一组随机字符串并对其进行排序。 I have looked online and in my book to try and model my code off of it and this is what I came up with.我已经在网上查看并在我的书中尝试使用 model 我的代码,这就是我想出的。 I am not sure where I went wrong, any help would be appreciated.我不确定我哪里出错了,任何帮助将不胜感激。

Here is how you load the array with the random strings:
string Sorter::randomString() {
    string s = "";
    for (int i = 0; i < MAX_CHARS; i++) {
        char randomChar = char(rand() % 26 + 97);
    s += randomChar;
    }
    return s;
}

void Sorter::load() {
    for (int i = 0; i < MAX_STRINGS; i++)
        workspace[i] = randomString();

Here is my selection sort: 

void Sorter::selectionSort() {
    for (int pass = 0; pass < MAX_STRINGS - 1; pass++) {
        string smallest = workspace[pass];
    for (int pos = pass + 1; pos < MAX_STRINGS - pass - 1; pos++) {
            if (workspace[pos] > smallest) {
                smallest = workspace[pos];
            }
            swap(workspace[pos], workspace[pass]);
        }
    }
}

I expected the array workspace to be sorted, but it is not:(我希望对数组工作区进行排序,但事实并非如此:(

There was a bit of a flaw in your logic in that you weren't setting the minimum element in the list properly.您的逻辑存在一些缺陷,因为您没有正确设置列表中的最小元素。 You should use a minimum index for this.您应该为此使用最小索引。

void selectionSort() {
    //Initialise minimum index
    int min_id = 0;
    //Loop through unsorted subarray 
    for (int pass = 0; pass < MAX_STRINGS - 1; pass++) {
        //Find the minimum element in rest of array
        min_id = pass;
        for (int pos = pass + 1; pos < MAX_STRINGS; pos++) {
            if (workspace[pos] < workspace[min_id]) {
                min_id = pos;
            }
        }
        //Swap the minimum element with current element in array
        swap(workspace[min_id], workspace[pass]);
    }
}

You couldn't find the smallest element in an unsorted array correctly.您无法正确找到未排序数组中的最小元素。 Here is my code:这是我的代码:

void selectionSort(char arr[][MAX_STRINGS], int n)
{
    int i, j, min_idx;

    // One by one move boundary of unsorted subarray  
    char minStr[MAX_STRINGS];
    for (i = 0; i < n - 1; i++)
    {
        // Find the minimum element in unsorted array  
        int min_idx = i;
        strcpy_s(minStr, arr[i]);
        for (j = i + 1; j < n; j++)
        {
            // If min is greater than arr[j]  
            if (strcmp(minStr, arr[j]) > 0)
            {
                // Make arr[j] as minStr and update min_idx  
                strcpy_s(minStr, arr[j]);
                min_idx = j;
            }
        }

        // Swap the found minimum element with the first element  
        if (min_idx != i)
        {
            char temp[MAX_STRINGS];
            strcpy_s(temp, arr[i]); //swap item[pos] and item[i]  
            strcpy_s(arr[i], arr[min_idx]);
            strcpy_s(arr[min_idx], temp);
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM