简体   繁体   English

修改选择排序算法,通过增加长度对字符串数组进行排序

[英]Modify the selection sort algorithm to sort an array of strings by increasing length

I've been given an assignment for class and I'm trying to do it but I confused at the wording我被分配了 class 的任务,我正在尝试这样做,但我对措辞感到困惑

Modify the selection sort algorithm to sort an array of strings by increasing length.修改选择排序算法,通过增加长度对字符串数组进行排序。

Now, this is from the book "Big C++ Late Objects Enhanced etext", 3rd edition, in practice exercises 12.1.现在,这是来自《Big C++ Late Objects Enhanced etext》一书,第 3 版,练习题 12.1。

I'm confused at the part "sort an array of strings by increasing length."我对“通过增加长度对字符串数组进行排序”部分感到困惑。 I don't know what they mean by "by increasing length."我不知道他们所说的“通过增加长度”是什么意思。 I thought that I had to mod the algorithm to where I had to create a variable and make it to where you Console In (or cin>>) to that variable when the programs run but I got a feeling that not what I'm supposed to do.我认为我必须将算法修改为我必须创建一个变量的位置,并在程序运行时将其设置为您在控制台输入(或 cin>>)到该变量的位置,但我有一种感觉,这不是我应该的去做。

After looking on some info on my problem along with the comments down below (thanks to the user for them btw) I was able to mod the program to this:在查看了有关我的问题的一些信息以及下面的评论(顺便感谢用户)之后,我能够将程序修改为:

#include <cstdlib>
#include <ctime>
#include <iostream>
#include <stdio.h>
#include <string>

using namespace std;

/**
    Gets the position of the smallest element in an array range.
    @param a the array
    @param from the beginning of the range
    @param to the end of the range
    @return the position of the smallest element in
    the range a[from]...a[to]
*/
int min_position(const char a[], int from, int to)
{
   int min_pos = from;
   for (int i = from + 1; i <= to; i++)
   {
      if (a[i] < a[min_pos]) { min_pos = i; }
   }
   return min_pos;
}

/**
   Swaps two integers.
   @param x the first integer to swap
   @param y the second integer to swap
*/
void swap(int& x, int& y)
{
   int temp = x;
   x = y;
   y = temp;
}

/**
   Sorts a array using the selection sort algorithm
   @param a the array to sort
   @param size the number of elements in a
*/
void selection_sort(const char a[], int size)
{
   int next; // The next position to be set to the minimum

   for (next = 0; next < size - 1; next++)
   {
      // Find the position of the minimum starting at next
      int min_pos = min_position(a, next, size - 1);
      // Swap the next element and the minimum
      swap(a[next], a[min_pos]);
   }
}

/**
   Prints all elements in an array.
   @param a the array to print
   @param size the number of elements in a
*/
void print(const char a[], int size)
{
   for (int i = 0; i < size; i++)
   {
      cout << a[i] << " ";
   }
   cout << endl;
}

int main()
{
  srand(time(0));
   const int SIZE = 6;
   // changed value to Words
   const char * Words[SIZE]={"School" , "To", "Sky" ,"Grade" , "A","Amazing"};



   for(unsigned i = 0; i < SIZE; i++)
   {
      cout<<Words[rand()%SIZE]<<endl;
   }

   print(Words, SIZE);
   selection_sort(Words, SIZE);
   print(Words, SIZE);
   return 0;
}

I made it so that the Words in the "Words" char type will be chosen at random and will continue to do so, sorting out the shortest word to the longest and printing them out.我这样做是为了随机选择“Words”字符类型中的单词并将继续这样做,将最短的单词排序为最长的单词并将它们打印出来。 Please note that is a work in progress at the moment.请注意,这是目前正在进行的工作。

The problem for me now is that I can't seem to call the functions of print , and selection_sort and I know that just the tip of the iceberg to what needs to be done.我现在的问题是我似乎无法调用printselection_sort的函数,我知道这只是需要做的事情的冰山一角。 I know I can use str.length to sort out the words but now I want to focus on calling the function so any advice is useful.我知道我可以使用str.length来整理单词,但现在我想专注于调用 function 所以任何建议都是有用的。

They probably mean the length of characters in the string.它们可能意味着字符串中字符的长度。 If you use a strings size() or length() method it will return the amount of characters in the string.如果您使用字符串 size() 或 length() 方法,它将返回字符串中的字符数。

If you use this function on every string you will have an array of ints that you can sort in ascending order.如果您在每个字符串上使用此 function,您将拥有一个可以按升序排序的整数数组。

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

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