简体   繁体   English

这是冒泡排序或插入排序在C?

[英]Is this Bubble sort or insertion sort in C?

I have written this code by understanding the insertion sort algo. 我通过理解插入排序算法编写了这段代码。 My teacher says its bubble sort but my friends are saying it is insertion. 我的老师说它的泡泡排序,但我的朋友说它是插入。 Could someone please check and brief me on this. 请有人请检查并向我介绍这一点。

#include <stdio.h>
void sort(int n) {
  int i, j;
  float arr[n], k;
  for (i = 0; i <= n - 1; i++) {
    printf("Enter the number");
    scanf("%f", &arr[i]);
  }

  for (i = 1; i <= n - 1; i++) {
    j=i
    while (arr[j] < arr[j - 1] && j > 0) {
      k = arr[j - 1];
      arr[j - 1] = arr[j];
      arr[j] = k;
      /*printf("\n\t%f",arr[j]);*/
      j--;
      /*printf("\n%d",j);*/
    }
    /*printf("\n%d",x[i]);*/
  }
  for (i = 0; i < n; i++) {
    printf("\n%f", arr[i]);
  }
}

int main() {
  int t;
  printf("Enter the number of values to be sorted");
  scanf("%d", &t);
  sort(t);
}

Looks like both/neither for me. 看起来两者兼而有之。 It may be insert sort, because you always take first element in unsorted part of array and puts it into correct place in sorted part of array. 它可能是插入排序,因为你总是在数组的未排序部分中取第一个元素并将其放入数组的排序部分中的正确位置。 However, insertion is not done by moving all necessary elements 1 element right and then inserting selected element to correct place (which i would say, standard insert sort does), but instead it swaps selected element with 1 element to the left, until selected element is in correct place. 但是,插入不是通过向右移动所有必需元素1元素然后将所选元素插入到正确的位置(我会说,标准插入排序),而是将所选元素与1个元素交换到左边,直到选中元素是在正确的地方。

Because of "sorted" and "unsorted" part of array, I would say insert sort. 由于数组的“已排序”和“未排序”部分,我会说插入排序。

Because of swapping neighbour elements, I would say bubble sort. 由于交换邻居元素,我会说冒泡排序。

However, it seems more like insert sort to me. 但是,它似乎更像是插入排序给我。 If instead of (inefficient) swapping, you would instead only move left element to right, and only finally write selected element to left (final, correct position of selected element), It would be a nice insert sort 如果不是(低效)交换,你只需要将左边元素移动到右边,最后只将所选元素写入左边(所选元素的最终,正确位置),这将是一个很好的插入排序

I would say closer to insertion sort as you create sorted chunks by finding the first misfit. 我会说接近插入排序,因为你通过找到第一个错配来创建排序的块。 Rather than iterating whole array and pushing the largest element to the end (like in bubble sort) 而不是迭代整个数组并将最大元素推送到最后(如在冒泡排序中)

It is closer to the standard version of insertion sort, where you swap elements until you find the right place in the sorted chunk. 它更接近插入排序的标准版本,您可以在其中交换元素,直到在排序的块中找到正确的位置。 However, after you insert your element you start your index from wrong index, as it has created a sorted sub array. 但是,插入元素后,从错误的索引开始索引,因为它创建了一个已排序的子数组。

This helps visualize the two sorting algorithms: https://visualgo.net/bn/sorting 这有助于可视化两种排序算法: https//visualgo.net/bn/sorting

This is a Bubble Sort algorithm. 这是一个Bubble Sort算法。 Only difference between Bubble Sort algorithm and your algorithm is that Bubble Sort algorithm sorts the rightmost elements first and then so on and your algorithm is sorting the leftmost elements first. Bubble Sort算法与您的算法之间的唯一区别是Bubble Sort算法首先对最右边的元素进行Bubble Sort ,然后依次对您的算法进行排序。

Analysis: 分析:

Your Algo: 你的Algo:

<---------
 <--------
  <-------
   <------
    <-----
     <----
      <---
       <--
        <-
         <

Bubble Sort Algo: 冒泡排序算法:

------>
----->
---->
--->
-->
->
>

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

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