bubble-sort

Bubble Sort Algorithm in C

半世苍凉 提交于 2019-11-30 10:03:15
问题 The program I"m trying to finish is a program using the bubble sort algorithm. I am not sure what is the problem or in which function the problem is in. The problem is the program does not sort the array in properly. (It also must be arranged in ascending order). Here is the code: #include <stdio.h> #include "simpio.h" void getArray (int arr[], int size); void sortArray (int arr[], int size); void swap (int arr[], int num, int number); void dispArray (int arr[], int size); bool checkBigger

How to find number of expected swaps in bubble sort in better than O(n^2) time

情到浓时终转凉″ 提交于 2019-11-30 09:47:30
问题 I am stuck on problem http://www.codechef.com/JULY12/problems/LEBOBBLE Here it is required to find number of expected swaps. I tried an O(n^2) solution but it is timing out. The code is like: swaps = 0 for(i = 0;i < n-1;i++) for(j = i+1;j<n;j++) { swaps += expected swap of A[i] and A[j] } Since probabilities of elements are varying, so every pair is needed to be compared. So according to me the above code snippet must be most efficient but it is timing out. Can it be done in O(nlogn) or it

How to find number of expected swaps in bubble sort in better than O(n^2) time

China☆狼群 提交于 2019-11-29 16:32:16
I am stuck on problem http://www.codechef.com/JULY12/problems/LEBOBBLE Here it is required to find number of expected swaps. I tried an O(n^2) solution but it is timing out. The code is like: swaps = 0 for(i = 0;i < n-1;i++) for(j = i+1;j<n;j++) { swaps += expected swap of A[i] and A[j] } Since probabilities of elements are varying, so every pair is needed to be compared. So according to me the above code snippet must be most efficient but it is timing out. Can it be done in O(nlogn) or it any complexity better than O(n^2). Give me any hint if possible. Alright, let's think about this. We

BubbleSort Implementation

╄→尐↘猪︶ㄣ 提交于 2019-11-29 09:01:25
I tried to make an implementation of bubble sort, but I am not sure whether it is correct or not. If you can give it a look and if it is a bubble sort and can be done in better way please don't be shy. Here is the code: package Exercises; import java.util.*; public class BubbleSort_6_18 { public static void main(String[] args) { Random generator = new Random(); int[] list = new int[11]; for(int i=0; i<list.length; i++) { list[i] = generator.nextInt(10); } System.out.println("Original Random array: "); printArray(list); bubbleSort(list); System.out.println("\nAfter bubble sort: "); printArray

VBA Bubble Sort Algorithm Slow

与世无争的帅哥 提交于 2019-11-29 08:59:22
I am surprised at how slow this bubble sort algorithm is using VBA. So my question is am I doing something wrong/inefficient, or is this just the best VBA and bubble sort will do? For instance, could using VARIANTs, too many variables, etc. be slowing performance substantially. I know Bubble Sort is not particularly fast, but I didn't think it would be this slow. Algorithm inputs: 2D array and either one or two columns to sort by, each asc or desc. I don't necessarily need lightning fast, but 30 seconds for 5,000 rows is completely unacceptable Option Explicit Sub sortA() Dim start_time, end

bubble sort a character array in alphabetic order in c

余生长醉 提交于 2019-11-28 12:17:35
问题 I'm trying to bubble sort a character array in alphabetic order. My code is as follows: #define CLASS_SIZE 10 #include <stdio.h> void bubbleSortAWriteToB(const char a[], char *b[]); int main(void){ char *s_letters[CLASS_SIZE]; char letters[CLASS_SIZE] = {'a','r','p','b','r','c','x','e','w','j'}; bubbleSortAWriteToB(letters,s_letters); return 0; } void bubbleSortAWriteToB(const char a[], char *b[]){ char temp; int i,j; for(i=0;i<CLASS_SIZE-1;i++){ for(j=1;j<CLASS_SIZE;j++){ if((int)a[j-1]>(int

What's the most elegant way to bubble-sort in C#?

你。 提交于 2019-11-28 09:26:58
Can this be cleaned up? using System; class AscendingBubbleSort { public static void Main() { int i = 0,j = 0,t = 0; int []c=new int[20]; for(i=0;i<20;i++) { Console.WriteLine("Enter Value p[{0}]:", i); c[i]=int.Parse(Console.ReadLine()); } // Sorting: Bubble Sort for(i=0;i<20;i++) { for(j=i+1;j<20;j++) { if(c[i]>c[j]) { Console.WriteLine("c[{0}]={1}, c[{2}]={3}", i, c[i], j, c[j]); t=c[i]; c[i]=c[j]; c[j]=t; } } } Console.WriteLine("bubble sorted array:"); // sorted array output for(i=0;i<20;i++) { Console.WriteLine ("c[{0}]={1}", i, c[i]); } } } Jon Skeet What you've pasted there isn't a

Why is bubble sort O(n^2)?

旧时模样 提交于 2019-11-28 09:06:26
int currentMinIndex = 0; for (int front = 0; front < intArray.length; front++) { currentMinIndex = front; for (int i = front; i < intArray.length; i++) { if (intArray[i] < intArray[currentMinIndex]) { currentMinIndex = i; } } int tmp = intArray[front]; intArray[front] = intArray[currentMinIndex]; intArray[currentMinIndex] = tmp; } The inner loop is iterating: n + (n-1) + (n-2) + (n-3) + ... + 1 times. The outer loop is iterating: n times. So you get n * (the sum of the numbers 1 to n) Isn't that n * ( n*(n+1)/2 ) = n * ( (n^2) + n/2 ) Which would be (n^3) + (n^2)/2 = O(n^3) ? I am positive I

Number of swaps in Bubble Sort

吃可爱长大的小学妹 提交于 2019-11-28 08:17:29
问题 I have a version of bubble sort: int i, j; for i from n downto 1 { for j from 1 to i-1 { if (A[j] > A[j+1]) swap(A[j], A[j+1]) } } I want to calculate the expected number of swaps using the above version of bubble sort. The method used by me is shown below : // 0 based index float ans = 0.0; for ( int i = 0; i < n-1; i++ ) { for ( int j = i+1; j < n; j++ ) { ans += getprob( a[i], a[j]); // computes probability that a[i]>a[j]. } } Am i going the correct way or am I missing something? 回答1: The

VBA Bubble Sort Algorithm Slow

大憨熊 提交于 2019-11-28 02:21:58
问题 I am surprised at how slow this bubble sort algorithm is using VBA. So my question is am I doing something wrong/inefficient, or is this just the best VBA and bubble sort will do? For instance, could using VARIANTs, too many variables, etc. be slowing performance substantially. I know Bubble Sort is not particularly fast, but I didn't think it would be this slow. Algorithm inputs: 2D array and either one or two columns to sort by, each asc or desc. I don't necessarily need lightning fast, but