私はかなり新しいコードを書いています。このコードでは、膨大な整数の配列をランダムに生成し、特定のシェルソートを選択して配列が正しくソートされました。私は私が間違ってやっているのか分からないシェル並べ替えと並べ替えの確認
#include <iostream>
#include <stdlib.h>
#include <time.h>
#define LISTLEN 100000
using namespace std;
void shellSort(int[], int, int[], int);
void testIfSorted(int[], int);
void main()
{
{
int list[LISTLEN];
int seq1[] = {LISTLEN/2};
int seq2[] = {2-1};
int seq3[] = { 4 + 3 * (2^0) + 1 };
int choice;
for (int i = 1; i < LISTLEN; i++)
{
list[i] = rand() % LISTLEN + 1;
cout << list[i] << endl;
}
cout << "\nEnter the Number for Which Sequence-Type You Wish to Use: \n"
"1) Shell Sequence\n"
"2) Hibbard Sequence\n"
"3) Sedgewick Sequence\n";
cin >> choice;
if (choice == 1)
{
shellSort(list, LISTLEN, seq1, LISTLEN/2);
}
else if (choice == 2)
{
shellSort(list, LISTLEN, seq2, (2 - 1));
}
else if (choice == 3)
{
shellSort(list, LISTLEN, seq3, (4 + 3 * (2^0) + 1));
}
cout << "\nVerifying List was Sorted\n";
testIfSorted;
}
}
void shellSort(int arr[], int arrsize, int seq[], int seqsize)
{
clock_t t;
t = clock();
{
int j, temp;
for (int i = seqsize; i < arrsize; i += 1)
{
temp = seq[i];
for (j = i; j >= seqsize && seq[j - seqsize] > temp; j -= seqsize)
{
seq[j] = seq[j - seqsize];
}
seq[j] = temp;
cout << temp << endl << endl;
}
t = clock() - t;
printf("It took me %d clicks (%f seconds).\n", t, ((float)t)/CLOCKS_PER_SEC);
}
}
void testIfSorted(int list[], int length)
{
for (int i = 0; i < length - 1; i++)
{
if (list[i] < list[i + 1])
{
cout << "List Isn't Sorted Correctly\n";
}
else (list[i] > list[i + 1]);
{
cout << "List Sorted Correctly\n";
}
}
}
、シェルソートは、実際には配列をソートしていないか、または少なくともそれは降順の並べ替えをしないと、プログラムは確認してくださいない場合配列が正しくソートされているため、表示したいメッセージが表示されません。
これは、誰かに 'if'文を忘れてしまったことを賢明な方法です:) –