标签: bubble-sort

在C#中进行冒泡排序的最优雅方法是什么?

可以清理吗?

using System;  
class AscendingBubbleSort 
{     
    public static void Main()
    {
        int i = 0,j = 0,t = 0;
        int []c=new int[20];
        for(i=0;i<20;i++)
        {
            Console.WriteLine("Enter Value p[{0}]:", i);
            c[i]=int.Parse(Console.ReadLine());
        }
        // Sorting: Bubble Sort
        for(i=0;i<20;i++)
        {
            for(j=i+1;j<20;j++)
            {
                if(c[i]>c[j])
                {
                    Console.WriteLine("c[{0}]={1}, c[{2}]={3}", i, c[i], j, c[j]);
                    t=c[i];
                    c[i]=c[j];
                    c[j]=t;
                }
            }
        }
        Console.WriteLine("bubble sorted array:");
        // sorted array output
        for(i=0;i<20;i++)
        {
            Console.WriteLine ("c[{0}]={1}", i, c[i]);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

.net c# arrays bubble-sort

6
推荐指数
3
解决办法
2万
查看次数

对于小案例,为什么插入排序比快速排序和冒泡排序更快?

我最近读了一篇文章,讨论了算法的计算复杂性.作者提到"为什么插入排序比小型案例的快速排序和冒泡排序更快".有人可以为此做出一些解释吗?

有人知道我上面提到的每种排序算法的实际复杂性吗?

algorithm quicksort bubble-sort time-complexity insertion-sort

6
推荐指数
1
解决办法
4522
查看次数

这个我的冒泡排序计划中的错误是什么?

我已经一次又一次检查代码中的任何问题,但无法弄清楚为什么我的气泡分拣程序没有给出正确的输出.你能帮我识别一下吗?

#include <iostream.h>
#include <conio.h>

 using namespace std;

 main()

{
  int number[10];
  int temp=0;
  int i=0;

  cout<<"Please enter any ten numbers to sort one by one: "<<"\n";

  for (i=0;i<10;i++)
  {
      cin>>number[i];
      }
  i=0;

  for (i=0;i<10;i++)
  {

      if(number[i]>number[i+1])

      {
      temp=number[i+1];
      number[i+1]=number[i];
      number[i]=temp;
                          }

      }
      i=0;
      cout<<"The sorted numbers are given below:"<<"\n";

      for (i=0;i<10;i++)
      {
        cout<<number[i]<<"\n";  
          }

          getch();
  }
Run Code Online (Sandbox Code Playgroud)

编辑:我已经接受了你们所说的必须有一个外循环.但我又在想我写的东西.我认为具有气泡条件的ONLY循环应该进行排序.这是我在想的:

for (i=0;i<10;i++)
    if(number[i]>number[i+1])
    {
        temp=number[i+1];
        number[i+1]=number[i];
        number[i]=temp;

    }
} 
Run Code Online (Sandbox Code Playgroud)

现在我解释一下我在想什么这个循环"应该"做什么.它首先将数字[0]与数字[1]进行比较.如果条件满足,它将执行IF语句的正文.然后我将增加1(i ++).然后在下一次迭代中,比较的值将是数字[1]和数字[2].那么为什么它不会发生并且循环仅在通过后退出?换句话说,可能是我试图要求IF语句不会在for循环中重复?在我看来它确实如此.我非常感谢你的帮助和观点,我的问题可能很小,但这就是我的进步方式.谢谢.

c++ sorting bubble-sort

6
推荐指数
1
解决办法
1310
查看次数

两个冒泡排序循环之间的实际区别

我的老师告诉我,这是Bubble Sort的唯一代码

int a[]={2,3,7,9,8,1,4,5,10,6};
     for(int i=0;i<a.length;i++)
     {
        for(int j=0;j<a.length-i-1;j++)
        {
            if(a[j]>a[j+1])
            {
                int t=a[j];
                a[j]=a[j+1];
                a[j+1]=t;
            }
        }
     }
     for(int i=0;i<a.length;i++)
     {
         System.out.print(a[i]+"\t");
     }
Run Code Online (Sandbox Code Playgroud)

但我用不同的外循环运行程序 -

int b[]={2,3,7,9,8,1,4,5,10,6};
     for(int i=0;i<b.length-1;i++)
     {
        for(int j=0;j<b.length-i-1;j++)
        {
            if(b[j]>b[j+1])
            {
                int t=b[j];
                b[j]=b[j+1];
                b[j+1]=t;
            }
         }
     }
     for(int i=0;i<b.length;i++)
     {
         System.out.print(b[i]+"\t");
     }
Run Code Online (Sandbox Code Playgroud)

输出是 - 第一案例 -

1   2   3   4   5   6   7   8   9   10
Run Code Online (Sandbox Code Playgroud)

第二个案例 -

1   2   3   4   5   6   7   8   9   10
Run Code Online (Sandbox Code Playgroud)

所以现在我被告知我的代码是错误的,即使我的输出是正确的.拜托,告诉我,我完全错了?

java bubble-sort

6
推荐指数
1
解决办法
92
查看次数

冒泡在C中使用指针的结构

我想使用冒泡排序算法和C中的指针对结构数组进行排序.我有一个汽车结构:

typedef struct{
    char model[30];
    int hp;
    int price;
}cars;
Run Code Online (Sandbox Code Playgroud)

我为12个项目分配内存:

cars *pointer = (cars*)malloc(12*sizeof(cars));
Run Code Online (Sandbox Code Playgroud)

并从文件中读取数据:

for (i = 0; i <number ; i++) {
    fscanf(file, "%s %i %i\n", (pointer+i)->model, &(pointer+i)->hp, &(pointer+i)->price);
}
Run Code Online (Sandbox Code Playgroud)

我将指针传递ptrbubbleSort函数:

bubbleSort(pointer, number);
Run Code Online (Sandbox Code Playgroud)

这是我的bubbleSort功能:

void bubbleSort(cars *x, int size) {
    int i, j;
    for (i=0;i<size-1;i++) {
    int swapped = 0;
    for (j = 0; j < size - 1 - i; j++) {
        if ( (x+i)->hp > (x+j+1)->hp ) {
            cars …
Run Code Online (Sandbox Code Playgroud)

c struct pointers bubble-sort

6
推荐指数
1
解决办法
5150
查看次数

Better function for collections

Answering a question in SO, I stumbled into this problem:

(def x [7 4 8 9 10 54 55 2 23 30 12 5])

(defn insert-x 
  ([sorted-coll x] 
   (insert-x sorted-coll x 
     (if (= (type sorted-coll) clojure.lang.PersistentVector) [] '())))

  ([sorted-coll x acc]
  (let [is-vector  (= (type sorted-coll) clojure.lang.PersistentVector)
        format-it  #(into (if is-vector [] '()) %)
        compare   (if is-vector < >)]
    (cond 
      (empty? sorted-coll) (format-it (cons x acc))

      (compare (peek sorted-coll) x) 
      (format-it (concat 
                   ((if is-vector identity reverse) sorted-coll) 
                   (conj acc x))) …
Run Code Online (Sandbox Code Playgroud)

collections clojure bubble-sort

6
推荐指数
2
解决办法
124
查看次数

在 ruby​​ 中递归地将块传递给方法

def bubble_sort_by nums
  do_it_again = false
  nums[0...-1].each_with_index do |item, index|
    if yield(nums[index], nums[index + 1]) > 0
      nums[index], nums[index + 1] = nums[index + 1], nums[index]
      do_it_again = true
    end
  end
  bubble_sort_by nums if do_it_again
  nums
end

bubble_sort_by(["hi","hello","hey"]) do |left,right|
  right.length - left.length
end
Run Code Online (Sandbox Code Playgroud)

程序基于块进行冒泡排序。在这种情况下,块按长度排序。所以,我得到一个本地跳转错误。花了我一点,但我想通了。当我递归调用该方法时,我没有给它块。但是我该怎么做呢?

ruby recursion block bubble-sort

5
推荐指数
1
解决办法
2094
查看次数

汇编 - 用于排序字符串的冒泡排序

我正在使用tasm编写程序集.我的任务是编写一个程序,使用冒泡排序按字母顺序对输入的字符串进行排序.防爆.如果你输入"你好",它应该写"ehllo".我已经写了乞求输入字符串并对它进行排序(我认为它工作直到它应该打印出结果的结尾,但最后它只是写了我的.data一次并完成它的工作)PS对不起英语

.model small
.stack 100h

.data
request     db 'This program is using bubblesort to get alphabetical order of your enterd string', 0Dh, 0Ah, 'Enter your string:', 0Dh, 0Ah, '$'
result      db 0Dh, 0Ah, 'Result:', 0Dh, 0Ah, '$'
buffer      db 100, ?, 100 dup (0)

.code

start:
MOV ax, @data                   
MOV ds, ax                      


MOV ah, 09h
MOV dx, offset request
int 21h


MOV dx, offset buffer           
MOV ah, 0Ah                     
INT 21h                         


MOV si, offset buffer           
INC si                          
MOV bh, [si]                    
INC …
Run Code Online (Sandbox Code Playgroud)

sorting assembly tasm bubble-sort

5
推荐指数
1
解决办法
8123
查看次数

运行 Ruby 冒泡排序

我正在编写冒泡排序代码,作为 Ruby 初学者课程的一部分。我知道这 (array.length - 1).times do |i|是不好的做法,因为我不需要每次都跑到数组的末尾。(在我的例子中,[5,4,3,2,1] 5 在第一次运行时移到最后,在第二次结束时 4 移动到正确的位置,等等,所以没有必要再次检查这些数字):

def bubble_sort(array)
  (array.length - 1).times do
    (array.length - 1).times do |i|
      array[i], array[i+1] = array[i+1], array[i] if array[i] > array[i+1]
    end 
  end
end

bubble_sort([5,4,3,2,1])
Run Code Online (Sandbox Code Playgroud)

有没有一种巧妙的方法来告诉该方法每次检查少一个数组元素?

ruby arrays bubble-sort

5
推荐指数
1
解决办法
190
查看次数

JavaScript:冒泡排序

我使用 JS 制作了一个冒泡排序算法(sorta)。有时它可以工作,但问题是它只迭代数组一次。这是我的代码:

function bubble(arr) {
  for (var i = 0; i < arr.length; i++) {
    if (arr[i] > arr[i + 1]) {
      var a = arr[i]
      var b = arr[i + 1]
      arr[i] = b
      arr[i + 1] = a
    }
  }
  return arr;
}
Run Code Online (Sandbox Code Playgroud)

javascript sorting algorithm bubble-sort

5
推荐指数
1
解决办法
4万
查看次数