bubble-sort

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

不羁岁月 提交于 2019-12-04 11:32:14
Closed . This question needs to be more focused. It is not currently accepting answers. Learn more . 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 to do it in an actual problem. Would anyone be willing to provide a brief example of how

JavaScript BubbleSort, how to improve its efficiency?

北战南征 提交于 2019-12-04 10:01:33
Have a bubblesort routine similar to this. I need to make it more efficient by stopping the loop when the array is sorted or if the array is already sorted. 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; } } } Before enter the inner loop, create a boolean to check if a swap occured inside the inner loop. When the there is no swap the array is sorted. function sortNumbers

Bubble sort of structures using pointers in C

丶灬走出姿态 提交于 2019-12-04 06:30:17
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 function: void bubbleSort(cars *x, int size) { int i, j; for (i=0;i<size-1;i++) { int swapped = 0; for (j

Why Bubble sort complexity is O(n^2)?

微笑、不失礼 提交于 2019-12-04 05:26:40
问题 As I understand, the complexity of an algorithm is a maximum number of operations performed while sorting. So, the complexity of Bubble sort should be a sum of arithmmetic progression (from 1 to n-1), not n^2. The following implementation counts number of comparisons: public int[] sort(int[] a) { int operationsCount = 0; for (int i = 0; i < a.length; i++) { for(int j = i + 1; j < a.length; j++) { operationsCount++; if (a[i] > a[j]) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } } } System.out

Bubble sort in c linked list [closed]

前提是你 提交于 2019-12-03 22:00:20
I need to do is read in an input file into a linked list. Part of the file is: NameA, 25 NameB, 33 NameC, 23 NameD, 39 And after i need to sort by the number (bubble sort) and write it to another file. Here is what i have: #include <stdio.h> #include <stdlib.h> #include <string.h> struct node{ char name[20]; int number; struct node *next; struct node *prev; }*head; int main(void) { struct node *temp; temp = malloc(sizeof(struct node)); temp->next = NULL; head = temp; FILE *ifp; char fnamer[100] = ""; char line[128]; // printf("\n\nPlease Enter the Full Path of the file: \n"); // scanf("%s",

Please explain the cut in the Bubblesort Prolog program?

♀尐吖头ヾ 提交于 2019-12-03 18:16:19
问题 I'm currently working trough the Bratko Prolog book and I am looking at the bubble-sort Program. I can't seem to figure out why the cut( ! ) is necessary. Say the cut isn't there, and Prolog would backtrack, how could it possibly find bad answers? Because if I leave the cut out of it, Prolog begins by giving me the correct answer but then also gives alternative bad answers. As I see it, how can swap ever return a non sorted list? And how is it possible that a non sorted list ever hits the

why is the time complexity of bubble sort's best case being O(n)

冷暖自知 提交于 2019-12-03 07:37:50
问题 I deduced the time complexity of bubble sort in its best case according to the mothod used in book ALGORITHMS 2.2. But the answer turned out to be O(n^2). Here's my derivation, hope anyone can help me find out where is wrong: public void bubbleSort(int arr[]) { for(int i = 0, len = arr.length; i < len - 1; i++) { for(int j = 0; j < len - i - 1; j++) { if(arr[j + 1] < arr[j]) swap(arr, j, j + 1); } } } Statements cost times i = 0,len = arr.length c1 1 i < len - 1 c2 n i++ c3 n - 1 j = 0 c4 n -

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-03 03:01:44
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 Well, don't know if it is the best algorithm available, but I can think of a O(n^2) solution: First, ignore the possibility of the cyclic array. Let's solve a simpler

Optimal bubble sorting algorithm for an array of arrays of numbers

纵饮孤独 提交于 2019-12-02 18:04:09
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 an optimal algorithm ? NOTE: There are many, many better sorting algorithms to use. However, for this

How to make Bubble Sort in Java to output the sorted numbers?

霸气de小男生 提交于 2019-12-02 16:19:46
问题 This is my code for the Bubble Sort. I cannot get the actual sorted values to output. The program reads the inputted numbers, but does not print it sorted. I'm not sure what I have to do to make them sort. Any advice or suggestions would be helpful. package sortingalgorithm2; import java.util.Scanner; public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { Scanner read = new Scanner (System.in); int[] num = new int[15]; int size = 15;