插入阵列排序方法
Insertion Array Sorting Method
我目前是一名研究插入排序方法的学生。以下是代码:
//Insertion Sorting of an Integer array
void InsertionSort(int insertVals[]){
//Systematic processing of the Array
for(int i = 0; i < INITSIZE - 1; i++){
//Value to check
int temp = insertVals[i];
//Index placeholder for the insterion sort
int k;
//Shifts the int array
for(k = i; k > 0 && insertVals[k-1] > temp; k--){
insertVals[k] = insertVals[k-1];
}
//Inserts the checked value back into the array
insertVals[k] = temp;
}
}
在我的测试中,我给了它从左到右的数组:
307 249 73 158 430 272 44 378 423 209
440 165 492 42 487 3 327 229 340 112
303 169 209 157 60 433 99 278 316 335
97 326 12 267 310 133 479 149 79 321
467 172 393 336 485 245 228 91 194 357
1 153 208 444 168 490 124 196 30 403
222 166 49 24 301 353 477 408 228 433
298 481 135 13 365 314 63 36 425 169
115 94 129 1 17 195 105 404 451 298
188 123 5 382 252 66 216 337 438 144
该方法从左到右产生:
314 63 314 63 36 425 36 169 425 169
115 115 94 129 94 129 1 17 195 105
404 451 298 188 123 5 382 252 66 216
337 438 144 1 17 195 105 404 451 298
188 123 5 382 252 66 216 337 438 144
228 229 245 249 252 267 272 278 298 298
301 303 307 310 314 316 321 326 327 335
336 337 340 353 357 365 378 382 393 403
404 408 423 425 430 433 433 438 440 444
451 467 477 479 481 485 487 490 492 144
我错误地编码了什么?
谢谢!
编辑:
//In main...
Printing(insertionSortValues, "Insertion Sorted Array");
//Function for Print
void Printing(int vals[], string s){
cout << s << ":" << endl;
for(int i = 0; i < INITSIZE; i++){
if(i % 10 == 0){
cout << endl;
}
cout << setw(3) << vals[i] << " ";
}
cout << endl;
}
这个问题的解决方案是通过@PaulMcKenzie解决的。线路:
for(int i = 0; i < INITSIZE - 1; i++){
需要成为:
for(int i = 0; i <= INITSIZE - 1; i++){
以下是修正后的函数。
//Insertion Sorting of an Integer array
void InsertionSort(int insertVals[]){
//Systematic processing of the Array
for(int i = 0; i <= INITSIZE - 1; i++){
//Value to check
int temp = insertVals[i];
//Index placeholder for the insterion sort
int k;
//Shifts the int array
for(k = i; k > 0 && insertVals[k-1] > temp; k--){
insertVals[k] = insertVals[k-1];
}
//Inserts the checked value back into the array
insertVals[k] = temp;
}
}
相关文章:
- 在 c++ 中对类中的 c 字符串动态数组进行排序的最佳方法是什么?
- 有没有更好的方法对C++中的三个整数进行排序?
- 获取位于特定区间的已排序值列表的子列表的最短方法
- 在 C++ 中,以相反顺序排序的最快方法是什么?
- 迭代快速排序方法的分区算法问题
- 查看数字是否包含在未排序的双数字间隔内的最有效方法?
- 多次对容器进行排序,使用什么容器和什么方法
- 随机化超大文件内容排序的有效方法是什么?
- 相对于另一个向量对一个向量进行排序 - 最有效的方法
- 我刚刚创建了一个非常快速的素数排序方法.如何改进?
- 在排序静态数组中搜索的最快方法
- 对这些 n^2 个数字进行排序的最快方法是什么
- 当有多个查询时,检查某些子数组是否排序的有效方法是什么?
- C++基数排序的有效方法
- 从排序的数字数组中返回数字范围的最快方法是什么?
- 改善并行bitonic排序的更好方法
- 模板集合的不同排序方法
- 根据这些字符串输入在句子中的位置对这些字符串输入进行排序的更快方法是什么
- STL 中的排序方法不能交换向量中的内容
- 在C std :: Priority_queue中反向字母排序顺序的简单方法