简体   繁体   English

为什么我的替代排序算法不起作用?

[英]Why didn't my alternative sorting algorithm work?

I was trying to implement a sorting code so i tried something completely without looking at any other reference codes or anything. 我试图实现一个排序代码,所以我完全尝试了一些事情,而没有查看任何其他参考代码或任何东西。 Please tell me why my code doesnt show any output? 请告诉我为什么我的代码不显示任何输出? It just runs for sometime and then stops suddenly. 它只运行了一段时间,然后突然停止。

I want to know where i am going wrong. 我想知道我要去哪里错了。 I wanted to take any array as an input and sort the numbers in it in ascending order. 我想将任何数组作为输入并按升序对数字进行排序。 So i just iterated throughput the array and compared adjacent elements and swaped them. 所以我只是迭代数组的吞吐量,比较相邻的元素并交换它们。 Then i tried printing the numbers using their indices but it did not print anything on the screen. 然后,我尝试使用其索引打印数字,但屏幕上未打印任何内容。

#include <iostream>
using namespace std;
int main() {
  int arr[6]={1,23,2,32,4,12};
  int i=0;
  while(i<6)
  {
    if(arr[i]>arr[i+1])
    {
      int x = arr[i];
      arr[i]=arr[i+1];
      arr[i+1]=x;
    }
    else
      continue;
    i++;
  }
  cout<<arr[0];
  cout<<arr[1];
  cout<<arr[2];
  cout<<arr[3];
  cout<<arr[4];
  cout<<arr[5];
  return 0;
}

I expected that the numbers will be printed in ascending order but nothing happened. 我期望数字将以升序打印,但是什么也没有发生。 And also tell me how to print an array all at once. 并告诉我如何一次打印一个数组。 Thank you 谢谢

Your sorting algorithm doesn't work because it performs only one pass of a variant of Bubble Sort or Insertion Sort. 您的排序算法不起作用,因为它仅执行一次冒泡排序或插入排序变体。 There has to be one more loop wrapped around your loop to repeat the operation N-1 times. 您的循环中必须再有一个循环,才能重复操作N-1次。

There are a number of ways to explain why multiple passes are required. 有很多方法可以解释为什么需要多次通过。 Here is one explanation. 这是一种解释。 Whenever we perform this step: 每当我们执行此步骤时:

if (arr[i] > arr[i+1])
{
  int x = arr[i];
  arr[i] = arr[i+1];
  arr[i+1] = x;
}

we transfer the arr[i+1] element into the arr[i] position. 我们将arr[i+1]元素转移到arr[i]位置。 By doing so, we effectively skip the arr[i+1] element. 这样,我们有效地跳过arr[i+1]元素。

Here is what I mean. 这就是我的意思。 Suppose arr[i] is called x , arr[i+1] is called y and arr[i+2] is called z . 假设arr[i]称为xarr[i+1]称为y,arr[i+2]称为z We start with xyz , and exchange x and y to make yxz . 我们从xyz开始,交换xy成为yxz But then on the next iteration of the loop, we compare x and z and have forgotten about y ; 但是,在循环的下一次迭代中,我们比较xz并忘记了y x has moved forward in the array, pushing down y . x在数组中向前移动,向下推y Why is that a problem? 为什么会有问题呢? Because y and z have not been compared; 因为yz没有被比较; and those two are not necessarily in sorted order! 而且这两个不一定按顺序排列!

Suppose we have { 3, 2, 0 }. 假设我们有{3,2,0}。 We compare 3 and 2, and swap them, giving us { 2, 3, 0}. 我们比较3和2,并交换它们,得到{2,3,0}。 Then we move on to comparing 3 and 0: we swap those and get { 2, 0, 3 }. 然后我们继续比较3和0:交换它们,得到{2,0,3}。 See the problem? 看到问题了吗? We neglected to deal with 2 and 0. That requires another pass through the array. 我们忽略了处理2和0。这需要再次遍历数组。

There is a family of algorithms which work by repeatedly scanning through an array and exchanging items. 有一系列算法可通过重复扫描数组并交换项目来工作。 I suggest studying the following of the common algorithms in this family: insertion sort , selection sort and Shell sort. 我建议研究以下该家族中的常见算法: 插入排序选择排序Shell排序。 Also look at bubble sort, in order to understand why it's a poor algorithm compared to either insertion or selection sort that it is closely related to. 还要看一下冒泡排序,以了解为什么它与紧密相关的插入排序或选择排序相比效果不佳。

  1. your loop condition is incorrect - the last iteration will compare arr[5] and arr[6] which gives index out of bound. 您的循环条件不正确-上一次迭代将比较arr [5]和arr [6],这将使索引超出范围。

  2. you update your iterator "i" only if you swap, so if your loop encounters the case where arr[i] <= arr[i+1], your iterator will never get updated and your program will run infinitely. 仅在交换时才更新迭代器“ i”,因此,如果循环遇到arr [i] <= arr [i + 1]的情况,则迭代器将永远不会更新,并且程序将无限运行。

  3. your algorithm implements just a first iteration of bubble sort ie it bubbles up (from left to right) the largest number and stops. 您的算法仅实现了冒泡排序的第一个迭代,即它(从左到右)冒泡了最大数量并停止。 so the final array looks like [1, 2, 23, 4, 12, 32] 所以最终的数组看起来像[1、2、23、4、12、32]

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

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