bubble-sort

Bubble sort on 2D Array Java

痴心易碎 提交于 2019-12-11 05:28:01
问题 String[][] 2dArray = new String[counter][2]; 2dArray [counter][column1] = String.valueOf(counter); 2dArray [counter][column2] = "something something something"; for(int i = 0; i < 2dArray.length-1; i++){ for(int j = i + 1; j > 0; j--){ if(2dArray[i][j] < 2dArray[i-1][j]){ int[][] temp = 2dArray[i-1][j]; 2dArray[i-1][j] = 2dArray[i][j]; 2dArray[i][j] = temp; } } } Attempting to sort the array so that column 1 is ascending. I've studied the other references on here and mimic'd them but for some

Bubble Sort in PHP and Python

旧巷老猫 提交于 2019-12-11 04:59:31
问题 As far as I can tell, these two programs should do exactly the same thing. However, the Python version works and the PHP one doesn't. What am I missing please? def bubbleSort(alist): for passnum in range(len(alist)-1,0,-1): for i in range(passnum): if alist[i]>alist[i+1]: temp = alist[i] alist[i] = alist[i+1] alist[i+1] = temp my_list = [2,3,5,4,1] bubbleSort(my_list) print(my_list) <?php // Bubble Sort $my_list = [2,3,5,4,1]; function bubble_sort($arr){ $size = count($arr); for($pass_num =

How to stop my process if I find the list in sorted in any intermediate point-IN BUBBLE SORT?

£可爱£侵袭症+ 提交于 2019-12-11 03:48:31
问题 #inlcude<stdio.h> int main() { int arr[30],num,i,j,k,temp,l=0; printf("Enter the number of elements :\n"); scanf("%d",&num); for(i=1;i<=num;i++) { printf("Enter element %d\n",i); scanf("%d",&arr[i]); } for(k=1;k<=num;k++) {if(arr[k]>arr[k+1]) l++;} if(l!=0) { for(i=2;i<=num;i++) {l=0; for(k=1;k<=num;k++) { if(arr[k]>arrk+1]) l++;} if(l!=0) {for(j=1;j<num;j++) { if(arr[j]>arr[j+1]) { temp=arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp; } } printf("\nAfter pass %d elements are:",i-1); for(k=1;k<=num;k+

C++ Bubble sorting a Doubly Linked List

混江龙づ霸主 提交于 2019-12-11 03:27:39
问题 I know bubble sort is probably not the fastest way to do this but its acceptable. i'm just having trouble with adjusting the algorithm to double link lists from arrays. My double linked lists have a type int and a type string to hold a number and a word. My list was sorted with an insertion sort that I wrote to sort alphabetically, now I need to reorder my double linked list numerically, greatest to least. My trouble spot is how to run this loop so that it is properly sorted thoroughly and

Bubble Sort Display

喜夏-厌秋 提交于 2019-12-11 01:05:45
问题 #include <iostream> #include <string> using namespace std; void bubbleSort(int data[], int n); int main() { cout << "Enter ten unsorted integers..." << endl; int a[10]; for (int i = 0; i < 10; ++ i) { cout << "[" << i << "] = "; cin >> a[i]; } cout << endl << "Unsorted List = "; for (int i = 0; i < 10; ++i) cout << a[i] << ", "; cout << endl; cout << "Sorting..." << endl; cout << "Sorted List = "; bubbleSort(a, 10); } void bubbleSort(int data[], int n) { int j = 0; bool nextEnd = true; while

C OpenMP parallel bubble sort

﹥>﹥吖頭↗ 提交于 2019-12-10 14:36:46
问题 I have an implementation of parallel bubble sort algorithm(Odd-Even transposition sort) in C, using OpenMP. However, after I tested it it's slower than the serial version(by about 10%) although I have a 4 cores processor ( 2 real x 2 because of Intel hyperthreading). I have checked to see if the cores are actually used and I can see them at 100% each when running the program. Therefore I think I did a mistake in the implementation the algorithm. I am using linux with kernel 2.6.38-8-generic.

Why is insertion sort faster than quick-sort and bubble-sort for small cases?

心已入冬 提交于 2019-12-10 13:38:59
问题 I recently read an article that talked about the computation complexity of algorithms. The author mentioned "why insertion sort is faster than quick-sort and bubble-sort for small cases". Could anybody make some explanation for that? Does anybody know the actual complexity of each sort algorithm I mentioned above? 回答1: Consider two complexity functions: F(X) = X^2 G(X) = 4 * X * ln(X) F(3) = 9 G(3) = 13 So algorithm F wins for 3 items. But: F(100) = 10,000 G(100) = 1,842 So algorithm G wins

given an array of integers in random order you have to find the minimum number of swaps to convert it to cyclic sorted array

ε祈祈猫儿з 提交于 2019-12-09 04:51:55
问题 if an array is given in random order , you have to output the minimum number of swaps required to convert into cyclic sorted array. e.g. array given is 3 5 4 2 1 so the first swap will be 5<-->4 result : 3 4 5 2 1 second swap will be 2<-->1 result : 3 4 5 1 2 (final) output : 2 i am not able to get the logic behind this problem. adding some more : swap only possible between adjacent elements and numbers are between range 1 to N 回答1: Well, don't know if it is the best algorithm available, but

Insertion sort better than Bubble sort?

这一生的挚爱 提交于 2019-12-08 15:41:10
问题 I am doing my revision for the exam. Would like to know under what condition will Insertion sort performs better than bubble sort given same average case complexity of O(N^2). I did found some related articles, but I can't understand them. Would anyone mind explaining it in a simple way? 回答1: The advantage of bubblesort is in the speed of detecting an already sorted list: BubbleSort Best Case Scenario: O(n) However, even in this case insertion sort got better/same performance. Bubblesort is,

There is another way to optimize this bubble-sort?

ぃ、小莉子 提交于 2019-12-08 10:15:42
问题 I have this code which sort a numeric array using bubble sort algorithm. var a = [34, 203, 3, 746, 200, 984, 198, 764, 9]; function bubbleSort(a) { var swapped; do { swapped = false; for (var i=0; i < a.length-1; i++) { if (a[i] < a[i+1]) { var temp = a[i]; a[i] = a[i+1]; a[i+1] = temp; swapped = true; } } } while (swapped); } bubbleSort(a); alert(a); You can see it here: http://jsfiddle.net/x7VpJ/ Well, it works perfect as you can check, but I wondered if there is another way to optimize