C++;-仅使用数组进行串排序 我有一个作业,在C++中创建一个 Stand排序< 算法,但是我不允许使用列表< /> >,只>代码>数组< /代码>。我很难理解这个算法,因为它从来没有向我解释过,而且谷歌在这个问题上提供的信息有限 我已经尽力把我找到的代码移植到PHP到C++,但是它不能正常工作。strong>它不会对数组进行排序。
这是我的代码,我知道很多代码可能写得很糟糕,但这是我所能做的最好的C++;-仅使用数组进行串排序 我有一个作业,在C++中创建一个 Stand排序< 算法,但是我不允许使用列表< /> >,只>代码>数组< /代码>。我很难理解这个算法,因为它从来没有向我解释过,而且谷歌在这个问题上提供的信息有限 我已经尽力把我找到的代码移植到PHP到C++,但是它不能正常工作。strong>它不会对数组进行排序。,c++,algorithm,sorting,C++,Algorithm,Sorting,这是我的代码,我知道很多代码可能写得很糟糕,但这是我所能做的最好的 #include <iostream> using namespace std; void echoArray(int a[], int n) { for(int i = 0; i < n; i++) cout << a[i] << " "; cout << endl; } //removes an element from array in
#include <iostream>
using namespace std;
void echoArray(int a[], int n) {
for(int i = 0; i < n; i++)
cout << a[i] << " ";
cout << endl;
}
//removes an element from array
int* array_remove(int* a, int& n, int index) {
int p = 0;
int* newArray = new int[n - 1];
for(int i = 0; i < n; i++) {
if(i != index) {
newArray[p] = a[i];
p++;
}
}
n--;
return newArray;
}
//adds an element to the end of an array
int* array_append(int* a, int& n, int el) {
int* newArray = new int[n+1];
int i = 0;
for(; i < n; i++) {
newArray[i] = a[i];
}
if(n == 0)
i = 0;
newArray[i] = el;
n++;
return newArray;
}
//inserts an element (el) to index p
int* array_insert(int* a, int& n, int p, int el) {
int c = 0;
n++;
int* newArray = new int[n];
for(int i = 0; i < n; i++) {
if(i != p) {
newArray[i] = a[c];
c++;
} else {
newArray[i] = el;
}
}
return newArray;
}
int* strandSort(int* a, int n) {
int arrC = n;
int resC = 0;
int subC = 0;
int* result = new int[1];
while(arrC > 0) {
subC = 0;
int* sublist = new int[1];
sublist = array_append(sublist, subC, a[0]);
a = array_remove(a, arrC, 0);
for(int i = 0; i < arrC; i++) {
if(a[i] > sublist[subC - 1]) {
sublist = array_append(sublist, subC, a[i]);
a = array_remove(a, arrC, i);
i--;
}
}
if(resC > 0) {
for(int i = 0; i < subC; i++) {
bool spliced = false;
for(int j = 0; j < resC - 1; i++) {
if(sublist[i] > result[j]) {
result = array_insert(result, resC, i, sublist[i]);
spliced = true;
break;
}
}
if(!spliced) {
result = array_append(result, resC, sublist[i]);
}
}
} else {
result = sublist;
resC = subC;
}
}
echoArray(result, resC);
return result;
}
int main() {
int a[] = {3, 20, 6, 1, 19, 21, 6, 11, 25, 6, 0, 1, 8, 7, 29, 26, 10, 29, 9, 5};
int n = 20;
strandSort(a, n);
return 0;
}
#包括
使用名称空间std;
无效回声阵列(int a[],int n){
对于(int i=0;i结果[j]){
结果=数组_插入(结果、resC、i、子列表[i]);
拼接=真;
打破
}
}
如果(!拼接){
结果=数组_append(结果、resC、子列表[i]);
}
}
}否则{
结果=子列表;
resC=subC;
}
}
回波阵列(结果,resC);
返回结果;
}
int main(){
INTA[]={3,20,6,1,19,21,6,11,25,6,0,1,8,7,29,26,10,29,9,5};
int n=20;
斯特兰德索特(a,n);
返回0;
}
另外,我意识到数组是通过引用传递的。使用cout
0){}
块中
仔细阅读代码
如果你仔细阅读你的代码,你会发现for(intj=0;j
中的i++
是毫无意义的
合并步骤有很多错误,你需要重新思考
步骤2的固定代码:
if(resC>0){
int j=0;
对于(int i=0;i
希望这能对您有所帮助@barakmanos,这不是完整的程序。我知道我犯了一些基本错误,我知道数组是通过引用传递的,但这不是我的问题所在。我使用的是返回值,因为赋值要求我这样做。由于函数最初不起作用,我没有费心返回他对数组进行了排序,但只是打印了数组,以查看结果。定义“不工作”。编译错误(如果是,包括确切的消息)?运行时错误?排序不正确(如果是,包括示例输入、预期和实际输出)?@Dukeling它不会对数组进行排序。谢谢!我现在明白了,我犯了一些愚蠢的错误。
cout << "step1: sublist = ";
echoArray(sublist, subC);
cout << "step2: result = ";
echoArray(result, resC);
step1: sublist = 3 20 21 25 29
step2: result = 3 20 21 25 29
step1: sublist = 6 19 26 29
step2: result = 6 19 26 29 3 20 21 25 29
step1: sublist = 1 6 11
step2: result = 6 19 11 26 29 3 20 21 25 29
step1: sublist = 6 8 10
step2: result = 6 8 10 19 11 26 29 3 20 21 25 29
step1: sublist = 0 1 7 9
step2: result = 6 8 7 9 10 19 11 26 29 3 20 21 25 29
step1: sublist = 5
step2: result = 6 8 7 9 10 19 11 26 29 3 20 21 25 29 0
if(resC > 0) {
int j = 0;
for(int i = 0; i < subC; i++) {
bool spliced = false;
for(;j < resC; j++) {
if(sublist[i] < result[j]) {
result = array_insert(result, resC, j++, sublist[i]);
spliced = true;
break;
}
}
if(!spliced) {
result = array_append(result, resC, sublist[i]);
}
}
} else {
result = sublist;
resC = subC;
}