[英]String Selection Sort in C++
需要一些有關字符串的幫助,以進行我的選擇排序。 到目前為止,這就是我所擁有的。
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
//Constant globals
const int NUM_NAMES = 20;
//Function protoypes
void selectionSort(string [], int);
void showArray(const string [] , int);
int main()
{
string names[NUM_NAMES] = {"Collins, Bill", "Smith, Bart", "Allet, Jim",
"Griffin, Jim", "Stamey, Marty", "Rose, Geri",
"Taylor, Terri", "Johnson, Jill",
"Aliison, Jeff", "Weaver, Jim", "Pore, Bob",
"Rutherford, Greg", "Javens, Renee",
"Harrison, Rose", "Setzer, Cathy",
"Pike, Gordon", "Holland, Beth"};
char again; //Hold y to repeat
do
{
//Show array
cout << "The unsorted values are\n";
showArray(names, NUM_NAMES);
//Sort array
selectionSort(names, NUM_NAMES);
//Display sorted array
cout << "The sorted values are\n";
showArray(names, NUM_NAMES);
//Run program again?
cout << "Would you like to run the program again? (Y/N): ";
cin >> again;
}while(again == 'y' || again == 'Y');
return 0;
}
更新了我的代碼,它運行完美。 將minValue從int更改為string。
void selectionSort(string array[], int NUM_NAMES)
{
int startScan, minIndex;
string minValue;
for(startScan = 0; startScan < (NUM_NAMES -1); startScan++)
{
minIndex = startScan;
minValue = array[startScan];
for(int index = startScan +1; index < NUM_NAMES; index++)
{
if (array[index] < minValue)
{
minValue = array[index];
minIndex = index;
}
}
array[minIndex] = array[startScan];
array[startScan] = minValue;
}
}
如果有人可以在這里幫助我,將不勝感激。
minValue = array[startScan];
您正在將字符串分配給int。 將minValue
的類型設置為string
。 然后它應該工作。
c ++和stl使選擇排序更加簡單和優雅(無論是字符串還是整數)
vector<string> v={"chandler","joey","phoebe","ross","rachel","monica"};
for(auto i=v.begin();i!=v.end();i++)
iter_swap(i, min_element(i,v.end()));
for(auto&x : v) cout<<x<<" ";//printing the sorted vector
如果您不使用c ++ 14,只需將auto更改為vector迭代器即可。
這可能會有所幫助:
import java.util.Arrays;
public class StringSort {
public static void main(String[] args){
final int nu_names = 20;
String names[ ] = new String[nu_names];
names = new String[]{"Collins, Bill", "Smith, Bert", "Allen, Jim",
"Griffin, Jim", "Stamey, Marty", "Rose, Geri",
"Taylor, Terri", "Johnson, Jim",
"Allison, Jeff", "Looney, joe", "Wolfe, Bill",
"James, Jean", "Weaver, Jim", "Pore, Bob",
"Rutherford, Greg", " Javens, Renee",
"Herrison, Rose", "Setzer, Cathy",
"Pike, Gordon", "Holland, Beth"};
sort(names);
System.out.print(Arrays.toString(names));// print names
}
//sort method
public static void sort(String[] names){
String currentName = "";
int currenIndex = 0;
for(int i = 0; i < names.length - 1; i++){
currentName = names[i];
currenIndex = i;
for(int j = i + 1; j < names.length; j++){
if(currentName.compareTo(names[j]) > 0) {
currentName = names[j];
currenIndex = j;
}
}
swap(names, i,currenIndex );
}
}
//Swap when necessary
public static void swap(String[] names, int a, int b){
String temp = names[a];
names[a] = names[b];
names[b] = temp;
}
}
您沒有對void showArray的任何定義。 我無法讓您的程序正常運行。 它不顯示名稱。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.