拥有与此类似的Bubblesort例程。我需要通过在对数组排序或对数组进行排序时停止循环来提高效率。
function sortNumbers(listbox) {
var x, y, holder;
// The Bubble Sort method.
for(x = 0; x < ranarray.length; x++) {
for(y = 0; y < (ranarray.length-1); y++) {
if(ranarray[y] > ranarray[y+1]) {
holder = ranarray[y+1];
ranarray[y+1] = ranarray[y];
ranarray[y] = holder;
}
}
}
Run Code Online (Sandbox Code Playgroud) 可能重复:
什么是气泡排序适合?
我确信每种算法都有其优点和缺点,那么与其他排序算法相比,buble sort怎么样呢?(当然我希望答案不是"易于学习")
我正在尝试将自己的冒泡算法编写为练习.我不明白这两个错误消息.谁能用我的代码指出问题?
// Bubble sort algorithm
#include <iostream>
#include <iomanip>
using namespace std;
void bubbleSort(int array[], int arraySize); // bubbleSort prototype
int main(void)
{
const int arraySize = 10;
int array[arraySize] = {2,3,6,5,7,8,9,3,7,4};
cout << "Unsorted: ";
for(int i = 0; i < arraySize; ++i)
cout << setw(5) << array[i];
cout << "Sorted: " << bubbleSort(array, arraySize);
}
void bubbleSort(int array[], int arraySize)
{
const int max = arraySize;
int swap = 0;
for(int i = 0; i < max; ++i) …Run Code Online (Sandbox Code Playgroud) 我正在尝试将Bubble排序方法实现为Ruby的简单编码问题,但我遇到了一些麻烦.我理解的想法是查看第一个元素的值并将其与第二个元素的值进行比较,然后相应地交换它们,但我似乎无法在实际问题中这样做.有人愿意提供一个关于这在Ruby中如何工作的简短示例吗?
ruby ruby-on-rails bubble-sort ruby-on-rails-3 ruby-on-rails-3.1
要使用冒泡排序算法对 6 个元素 {11,5,7,3,2,1} 的列表进行排序,您可以手动找到它有 14 次交换。我知道下面的公式可以进行比较
n(n-1)/2
Run Code Online (Sandbox Code Playgroud)
6(6-1)/2 = 15。为什么是 15 而不是 14?
另外,快速排序和插入排序是否有类似的公式?
提前致谢!
我看到了一个冒泡排序代码,最初我认为代码是错误的.但是在编译和运行之后,它让我惊讶它确实有效.我想知道第一个for循环中的第二个语句如何不是条件而是赋值.另外,为什么这段代码不会进入无限循环?
PS:它会产生一个警告:"建议围绕赋值的括号用作真值[-Whatarentheses]"抱怨第一个for循环.令人惊讶的是,这不是一个错误.
#include <iostream>
void bubblesort(int A[], int n)
{
for (bool sorted = false; sorted = !sorted; n--)
{
for (int i = 1; i < n; ++i)
{
if (A[i-1] > A[i])
{
int tmp = 0;
tmp = A[i];
A[i] = A[i-1];
A[i-1] = tmp;
sorted = false;
}
}
}
}
int main()
{
int a[5] = {1,4,5,2,3};
bubblesort(a, 5);
for (unsigned int i = 0; i < 5; ++i)
{
std::cout << a[i] << std::endl; …Run Code Online (Sandbox Code Playgroud) 首先:
for(int i=0;i<n-1;i++)
for(int j=n-1; j>i;j--)
if(a[j] < a[j-1])
swap(a[j], a[j-1]);
Run Code Online (Sandbox Code Playgroud)
或者第二个:
for(int i=0; i<n-1; i++)
for(int j=i+1; j<n; j++)
if(a[j] < a[i])
swap(a[j],a[i]);
Run Code Online (Sandbox Code Playgroud)
或第三版:
int temp, i, j = 0;
boolean swaped = true;
while (swaped) {
swaped = false;
j++;
for(i = 0; i < arr.length - j; i++){
if(arr[i] > arr[i+1]){
temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
swaped = true;
}
}
}
Run Code Online (Sandbox Code Playgroud)
有人说第一个,有人说第二个.那么哪一个是对的?有人说第二种是交换排序.很多书说冒泡排序是第三个版本,但很多人称第一个版本是冒泡排序.任何意见?
我试图排序一些用空格分隔的用户输入的整数.
输入:4 2 1 5 9 - 预期输出:1 2 4 5 9
用户在循环中按下回车后我无法弄清楚如何停止循环,其中i <num.当我逐个输入整数时,我的代码会起作用.任何帮助,将不胜感激
class javasort {
public static void main(String[] args) {
int num, i, j, temp;
Scanner input = new Scanner(System.in);
// System.out.println("Enter the number of integers to sort:");
// num = input.nextInt();
num = 5; // <-- the user input should be dynamic
int array[] = new int[num];
System.out.println("Enter integers: ");
for (i = 0; i < num; i++)
array[i] = Integer.parseInt(input.next());
num = i; // make …Run Code Online (Sandbox Code Playgroud) 使用javascript,它被编程为按照asc顺序对数组中的元素进行排序.我尽力理解内循环使用的原因length-i-1,但不能.谁能帮助我理解我们为什么要使用它?
function bubbleSort(arr) {
for(let i=0; i<= arr.length; i++) {
for(let j=0; j< arr.length-i-1; j++) {
if(arr[j] > arr[j+1]) {
let lesser = arr[j+1];
arr[j+1] = arr[j];
arr[j] = lesser;
}
}
}
return arr;
}
Run Code Online (Sandbox Code Playgroud) 我必须实现冒泡排序功能(排序算法)。
我已经实现了bubblesort和swap,一个帮助功能bubblesort:
swap([X,Y|T1],[Y,X|T1]):-(Y<X,!).
swap([X|T1],[X|T2]):- swap(T1,T2).
bubblesort([],[]) :- !.
bubblesort(T1,T2) :- (bubblesort(swap(T1,T2),T2)).
Run Code Online (Sandbox Code Playgroud)
我得到一个无限循环。我必须保留函数的签名:
冒泡排序(T1,T2)
我在这个问题上纠结了2个小时。有谁知道我该怎么做?
bubble-sort ×10
sorting ×5
algorithm ×4
c++ ×3
javascript ×2
c ×1
for-loop ×1
java ×1
performance ×1
prolog ×1
ruby ×1