bubble-sort

Is this code a correct implementation of Bubble Sort?

守給你的承諾、 提交于 2020-01-06 05:25:10
问题 I am used following bubble sort algorithm for making sorting . Is this algorithm correct? for (int a = itemWiseBidderList.size() - 1; a > 1; a--) { for (int j = 0; j < a; j++) { if ((itemWiseBidderList.get(j).getRankInInt()) > (itemWiseBidderList.get(j + 1).getRankInInt())) { Collections.swap(itemWiseBidderList, j, j + 1); } } } 回答1: If bubble sorting is not a requirement (by homework?), then the correct way to implement sorting in Java is by calling Collections.sort(itemWiseBidderList); If

Java Bubble Sort wrong output

不打扰是莪最后的温柔 提交于 2020-01-04 18:17:46
问题 I'm trying to implement a basic java bubble sort but I'm getting a wrong input.. Code is: public class BubbleSort{ public static void main(String args[]){ int [] arr_sort=new int [] {5, 10, 50, 32, 52, 25}; System.out.println("Bubble Sort"); System.out.println("Before sorting: "); int x; for (x=0; x<6; x++){ System.out.print(arr_sort[x] + " "); } System.out.println(); System.out.println("After Sorting"); int n = arr_sort.length; int temp = 0; for(int i=0; i<n; i++){ for(int j=1; j<(n-1); j++)

Java Bubble Sort wrong output

这一生的挚爱 提交于 2020-01-04 18:17:10
问题 I'm trying to implement a basic java bubble sort but I'm getting a wrong input.. Code is: public class BubbleSort{ public static void main(String args[]){ int [] arr_sort=new int [] {5, 10, 50, 32, 52, 25}; System.out.println("Bubble Sort"); System.out.println("Before sorting: "); int x; for (x=0; x<6; x++){ System.out.print(arr_sort[x] + " "); } System.out.println(); System.out.println("After Sorting"); int n = arr_sort.length; int temp = 0; for(int i=0; i<n; i++){ for(int j=1; j<(n-1); j++)

Erlang List concatenation, weird “|” sign

[亡魂溺海] 提交于 2020-01-04 01:57:07
问题 in order to become familar with Erlang, i'm trying to write my own Bubblesort Algorithm. Right now, i have the following code in my module: -module(mysort). -export([bubblesort/1]). bubblesort(L) -> sort_sequence(L, []). sort_sequence([H1|[H2|T]], Sorted) -> if H2 >= H1 -> sort_sequence(T, Sorted ++ [H1, H2]); H2 < H1 -> sort_sequence(T, Sorted ++ [H2, H1]) end; sort_sequence([H|T], Sorted) -> Sorted ++ H; sort_sequence([], Sorted) -> Sorted. first of all: please don't give me suggestions to

BubbleSorting C language

拜拜、爱过 提交于 2020-01-03 05:23:11
问题 We are learning arrays and I just came around bubble sorting. I wrote the following code to sort the array in ascending order but there is a problem. I can't find it out but I know there's a problem. I have found the correct code but I still don't understand why this is not working. My Code int a; int i; int temp; int value[5] = { 5, 4, 3, 2, 1 }; for (i = 0; i < 5; i++) { if (value[i] > value[i + 1]) { temp = value[i]; value[i] = value[i + 1]; value[i + 1] = temp; } } for (a = 0; a < 5; a++)

BubbleSorting C language

岁酱吖の 提交于 2020-01-03 05:23:09
问题 We are learning arrays and I just came around bubble sorting. I wrote the following code to sort the array in ascending order but there is a problem. I can't find it out but I know there's a problem. I have found the correct code but I still don't understand why this is not working. My Code int a; int i; int temp; int value[5] = { 5, 4, 3, 2, 1 }; for (i = 0; i < 5; i++) { if (value[i] > value[i + 1]) { temp = value[i]; value[i] = value[i + 1]; value[i + 1] = temp; } } for (a = 0; a < 5; a++)

Using the Bubble sort method for an array in Ruby [closed]

折月煮酒 提交于 2020-01-01 12:00:08
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 2 years ago . I'm trying to implement the Bubble sort method into an easy coding problem for Ruby, but I'm having some trouble. I understand the idea is to look at the value of the first element and compare it to the value of the second element and then swap them accordingly, but I can't seem

Bubble sort of structures using pointers in C

可紊 提交于 2020-01-01 10:02:18
问题 I want to sort an array of structures using the bubble sort algorithm and pointers in C. I have a cars structure: typedef struct{ char model[30]; int hp; int price; }cars; and I allocate memory for 12 items: cars *pointer = (cars*)malloc(12*sizeof(cars)); and read data from file: for (i = 0; i <number ; i++) { fscanf(file, "%s %i %i\n", (pointer+i)->model, &(pointer+i)->hp, &(pointer+i)->price); } I pass pointer ptr to bubbleSort function: bubbleSort(pointer, number); Here is my bubbleSort

Optimal bubble sorting algorithm for an array of arrays of numbers

自作多情 提交于 2019-12-31 09:11:38
问题 Fix positive integers n and k . Let A be an array of length n with A[i] an array of length k where every entry is n-i . For example, with n=5 and k=1 , this is just [ [5] , [4] , [3] , [2] , [1] ] and for n=5 and k=2 , this is [ [5,5] , [4,4] , [3,3] , [2,2] , [1,1] ] The goal is to bubble sort this array of arrays by swapping numbers in adjacent arrays (e.g. swap A[i][j1] with A[i+1][j2] ) until every entry of A[i] is i+1 for every i . The question is: how many swaps are necessary and what's

Simple bubble sort c#

时间秒杀一切 提交于 2019-12-27 11:47:03
问题 int[] arr = {800,11,50,771,649,770,240, 9}; int temp = 0; for (int write = 0; write < arr.Length; write++) { for (int sort = 0; sort < arr.Length - 1; sort++) { if (arr[sort] > arr[sort + 1]) { temp = arr[sort + 1]; arr[sort + 1] = arr[sort]; arr[sort] = temp; } } Console.Write("{0} ", arr[write]); } All I am attempting to do is a simple bubble sort with this array. I would like to figure out why the sorting is screwed up. In example, here is when the array is {800,11,50,771,649,770,240, 9} :