bubble-sort

Javascript: Bubble Sort

本小妞迷上赌 提交于 2020-07-05 11:24:10
问题 I have made a bubble sort algorithm (sorta) using JS. It works sometimes, but the problem is that it only iterates through the array once. Here is my code: 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; } 回答1: You need an inner loop to complete the sort correctly: function bubble(arr) { var len = arr.length; for (var i = 0; i < len ; i++) { for(var j = 0 ; j < len - i - 1; j++

Javascript: Bubble Sort

我只是一个虾纸丫 提交于 2020-07-05 11:24:05
问题 I have made a bubble sort algorithm (sorta) using JS. It works sometimes, but the problem is that it only iterates through the array once. Here is my code: 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; } 回答1: You need an inner loop to complete the sort correctly: function bubble(arr) { var len = arr.length; for (var i = 0; i < len ; i++) { for(var j = 0 ; j < len - i - 1; j++

Bubble sort on array on Assembly Language

安稳与你 提交于 2020-04-11 07:00:30
问题 I need to Bubblesort an unorganized array with 7 integers from biggest to smallest so it would look like 9,6,5,4,3,2,1. I ran my code through the compiler and it says I can't understand what is the problem with this code: code segment assume ds:code,cs:code start: mov ax,code mov ds,ax ;code start ARR: dw 1,2,4,3,6,5,9 mov ch,0h mov cl,1h mov bh 7h jmp assign_nums restart: mov ch,0h mov cl,1h dec bh jmp assign_nums swap: mov ch,dl mov cl,dh jmp next next: cmp bh,cl je restart add ch,1h add cl

i cant get bubble sorting to work in function while passing array to the function [closed]

て烟熏妆下的殇ゞ 提交于 2020-03-05 03:13:32
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 25 days ago . #include <stdio.h> #include <conio.h> void ascending(int numbers[], int size); int main() { int size=10, numbers[size], i, order; for (i=0; i<10; i++) { printf("please enter a number:"); scanf("%d", &numbers[i]); } ascending(numbers[], size); } void ascending(int numbers[], int size) { int temp, i, sflag, count

Bubble Sort in C#

老子叫甜甜 提交于 2020-02-02 16:00:34
问题 I am studying algorithms and I have two bubble sort functions/methods and both of them provide similar result. Can please someone tell me something more about them, such as performance etc? public void BubbleSort() { int temp; for (int outer = upper; outer >= 1; outer--) { for (int inner = 0; inner <= outer - 1; inner++) { if ((int)arr[inner] > arr[inner + 1]) { temp = arr[inner]; arr[inner] = arr[inner + 1]; arr[inner + 1] = temp; } } } } public void BubbleSor2() { int temp; for (int outer =

Bubble Sort in C#

泄露秘密 提交于 2020-02-02 15:56:12
问题 I am studying algorithms and I have two bubble sort functions/methods and both of them provide similar result. Can please someone tell me something more about them, such as performance etc? public void BubbleSort() { int temp; for (int outer = upper; outer >= 1; outer--) { for (int inner = 0; inner <= outer - 1; inner++) { if ((int)arr[inner] > arr[inner + 1]) { temp = arr[inner]; arr[inner] = arr[inner + 1]; arr[inner + 1] = temp; } } } } public void BubbleSor2() { int temp; for (int outer =

Java Bubblesort Algorithm

笑着哭i 提交于 2020-01-16 09:36:33
问题 I am trying to use the summer to practice more Java to get better by learning how to code algorithms. I have this problem where I add elements to my ArrayList but somehow the first number I add also sets the number of positions in my list which I want to avoid. I only want the 0th index to contain the number 5. I seem to not catch a clue on how to solve this. public class Algorithms { private ArrayList<Integer> numbers; public Algorithms() { numbers = new ArrayList<Integer>(); numbers.add(5);

Assembly - bubble sort for sorting string

与世无争的帅哥 提交于 2020-01-09 05:36:48
问题 I am writing a program in assembly using tasm. My task is to write a program that will use bubble sort to sort entered string alphabetically. Ex. if you enter "hello" it should write "ehllo". I have writened the begging to enter string and to sort it (I think it works okey until the end where it should print out the result, but at the end it just writes my .data once and the finisheds its work) P.S sorry for bad english .model small .stack 100h .data request db 'This program is using