bubble-sort

Simple bubble sort c#

被刻印的时光 ゝ 提交于 2019-12-27 11:45:26
问题 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} :

Bubble sort adaptive (java)

♀尐吖头ヾ 提交于 2019-12-25 18:19:55
问题 I'm writing up the Bubble Sort algorithm with worst case runtime of O(n^2) and best case of O(n) so that it is adaptive. My idea was to add some sort of boolean flag variable to control the while loop so that the algorithm would stop early if it was sorted early. However, it keeps failing my JUnit testing. I think it's the way I'm implementing the boolean variable but I'm not sure where else to put it. Any contributions would be greatly appreciated. public static<T> void bubbleSort(T[] arr,

Why cant you have one loop for bubble sort?

假装没事ソ 提交于 2019-12-24 13:45:05
问题 I was just in an argument with my instructor about bubble sort, he told me that bubble sort is known as two for loops, one nested in the other. Which was not given before I started the assignment, so okay that is fine, but what is wrong with this code for a bubble sort: int num = 0, i = 0; bool go = true; while (i < size - 1){ if (array[i] > array[i + 1]){ num = array[i]; array[i] = array[i + 1]; array[i + 1] = num; go = false; } i++; if (i >= size - 1 && go == false){ i = 0; go = true; } }

How can I sort arrays in a pointer array in a descending order using bubble sorts or selection sort?

女生的网名这么多〃 提交于 2019-12-24 07:39:53
问题 I am working on a project that sorts arrays inside the pointer arrays in a few different ways, though I am stuck on one way of sorting. The arrays are built in a way that the first number indicates the amount of numbers after it. For example, (3,0,23,1): this array has 3 numbers after the first index). I want to sort the array from lowest to highest number but I don't want to change the first index meaning the array will look like this (3,0,1,23). These are the arrays and the pointer array:

How can i use Comparator to implement Bubble sort?

时光毁灭记忆、已成空白 提交于 2019-12-24 07:14:34
问题 How can i implement Bubble sort by using Comparator? Thank you. This is how my comparator looks: class ColumnSorter implements Comparator { int colIndex; ColumnSorter(int colIndex) { this.colIndex = colIndex; } public int compare(Object a, Object b) { Vector v1 = (Vector) a; Vector v2 = (Vector) b; Object o1 = v1.get(colIndex); Object o2 = v2.get(colIndex); if (o1 instanceof String && ((String) o1).length() == 0) { o1 = null; } if (o2 instanceof String && ((String) o2).length() == 0) { o2 =

Bubble-Sort with 2D Array

三世轮回 提交于 2019-12-24 04:34:08
问题 Howdy, I do know how to implement a simple bubble-sort for 1dimensional array. But with 2dimensional or multidimensional, that's where I have my problems. So far I've been using this to sort 1Dimensional Arrays, works like a charm. But mostly with integer numbers, not strings: boolean sort; do{ sort = true; for (int i = 0; i < testarray.length - 1; i++){ if(testarray[i] > testarray[i+1]){ temp = testarray[i]; testarray[i] = testarray[i+1]; testarray[i+1] = temp; sort = false; } } }while(!sort

Bubble-sorting doubly linked list

你。 提交于 2019-12-24 03:30:33
问题 I have a problem with my bubble-sorting function for the doubly linked list. It is working when I'm sorting the nodes in the singly linked way (only with ->next), but I can't make it work with ->prev pointers. Here is the code I'm using: void sort(int count) { struct data *tmp,*current,*nextone; int i,j; for(i=0;i<count;i++) { current = first; for(j=0;j<count-1-i;j++ ) { if(current->number > current->next->number) { nextone = current->next; current->next = nextone->next; nextone->next =

Sorting 2D String Array with BUBBLESORT in java

时光总嘲笑我的痴心妄想 提交于 2019-12-23 19:29:50
问题 Similar questions have been asked but never about 2D String Arrays, therefore after trying for a long time I couldn't find what I wanted. I'm trying to sort a 2D String Array in java using BubbleSort. As input, I receive a two-dimensional array (a table) of Strings and the index of the “column” you should sort. I should sort the rows by the values in the indicated column. You can see the first index as a row index, and the second index as a column index. For example, the following Java array

Bubble sort using R language?

非 Y 不嫁゛ 提交于 2019-12-22 07:47:29
问题 I am new in programming, and I just start learning R language. I am trying to do a bubble sort, but it shows the following error message. Can anyone help me solve the problem? x <-sample(1:100,10) n <- length(x) example <- function(x) { for (i in 1:n-1) { while (x[i] > x[i+1]) { temp <- x[i+1] x[i+1] <- x[i] x[i] <- temp } i <- i+1 } } example(x) Error in while (x[i] > x[i + 1]) { : argument is of length zero 回答1: x<-sample(1:100,10) example <- function(x){ n<-length(x) for(j in 1:(n-1)){ for

Bubble sort using R language?

我与影子孤独终老i 提交于 2019-12-22 07:47:11
问题 I am new in programming, and I just start learning R language. I am trying to do a bubble sort, but it shows the following error message. Can anyone help me solve the problem? x <-sample(1:100,10) n <- length(x) example <- function(x) { for (i in 1:n-1) { while (x[i] > x[i+1]) { temp <- x[i+1] x[i+1] <- x[i] x[i] <- temp } i <- i+1 } } example(x) Error in while (x[i] > x[i + 1]) { : argument is of length zero 回答1: x<-sample(1:100,10) example <- function(x){ n<-length(x) for(j in 1:(n-1)){ for