bubble-sort

Sorting in arrays

偶尔善良 提交于 2019-12-02 12:51:11
问题 While sorting an array for ex: A[5]={1,4,5,3,2} the output must be 1,2,3,4,5 in ascending order. in using the concept of bubble sorting my output is 0,1,2,3,4 what would be the problem in my code int A[5]={1,5,3,2,4}; for(int i=0;i<5;i++){ for(int j=0;j<5;j++){ if(A[j]>A[j+1]) { int t=A[j]; A[j]=A[j+1]; A[j+1]=t; } } } for(i=0;i<5;i++) cout<<A[i]; 回答1: You need to limit your inner loop to <4: int A[5]={1,5,3,2,4}; for(int i=0;i<5;i++){ for(int j=0;j<4;j++){ if(A[j]>A[j+1]) { int t=A[j]; A[j]

bubble sort with a boolean to determine whether array is already sorted

自作多情 提交于 2019-12-02 07:46:05
I have following code for bubble sort but its not sorting at all. if I remove my boolean then its working fine. I understand that since my a[0] is lesser than all other elements therefore no swapping is being performed can anybody help me with this. package com.sample; public class BubleSort { public static void main(String[] args) { int a[] = { 1, 2, 4, 5, 6, 88, 4, 2, 4, 5, 8 }; a = sortBuble(a); for (int i : a) { System.out.println(i); } } private static int[] sortBuble(int[] a) { boolean swapped = true; for (int i = 0; i < a.length && swapped; i++) { swapped = false; System.out.println(

Sorting in arrays

让人想犯罪 __ 提交于 2019-12-02 07:00:00
While sorting an array for ex: A[5]={1,4,5,3,2} the output must be 1,2,3,4,5 in ascending order. in using the concept of bubble sorting my output is 0,1,2,3,4 what would be the problem in my code int A[5]={1,5,3,2,4}; for(int i=0;i<5;i++){ for(int j=0;j<5;j++){ if(A[j]>A[j+1]) { int t=A[j]; A[j]=A[j+1]; A[j+1]=t; } } } for(i=0;i<5;i++) cout<<A[i]; You need to limit your inner loop to <4: int A[5]={1,5,3,2,4}; for(int i=0;i<5;i++){ for(int j=0;j<4;j++){ if(A[j]>A[j+1]) { int t=A[j]; A[j]=A[j+1]; A[j+1]=t; } } } for(i=0;i<5;i++) cout<<A[i]; Why not use the STL sort? #include <algorithm> std:

Efficiency of Bubble vs. Selection Sort

笑着哭i 提交于 2019-12-02 04:33:57
问题 I understand that the big O values for Bubble Sort and Selection Sort are the same, (n)^2, but when I try to run both with an array of size 1000, the Bubble Sort takes 962037 swaps to sort the array, while Selection Sort only takes 988 swaps to sort the array. Why are these different? 回答1: Because the complexity refers to the number of comparisons, not the number of swaps. Both need O(n^2) comparisons, but selection sort needs only n-1 swaps in the worstcase (O(n)), whereas bubblesort might

Python: How can I make my implementation of bubble sort more time efficient?

﹥>﹥吖頭↗ 提交于 2019-12-02 01:45:44
问题 Here is my code - a bubble sort algorithm for sorting list elements in asc order: foo = [7, 0, 3, 4, -1] cnt = 0 for i in foo: for i in range(len(foo)-1): if foo[cnt] > foo[cnt + 1]: temp = foo[cnt] c[cnt] = c[cnt + 1] c[cnt + 1] = temp cnt = cnt + 1 cnt = 0 I've been revising my code, but it is still too inefficient for an online judge. Some help would be greatly appreciated! 回答1: Early Exit BubbleSort The first loop has no bearing on what happens inside The second loop does all the heavy

Efficiency of Bubble vs. Selection Sort

情到浓时终转凉″ 提交于 2019-12-02 01:20:27
I understand that the big O values for Bubble Sort and Selection Sort are the same, (n)^2, but when I try to run both with an array of size 1000, the Bubble Sort takes 962037 swaps to sort the array, while Selection Sort only takes 988 swaps to sort the array. Why are these different? Because the complexity refers to the number of comparisons, not the number of swaps. Both need O(n^2) comparisons, but selection sort needs only n-1 swaps in the worstcase (O(n)), whereas bubblesort might need up to n*(n-1)/2 swaps (O (n^2)). And even if the complexity would refer to the number of swaps - as the

Bubble sort universal implementation in C

那年仲夏 提交于 2019-12-01 21:41:59
I'm trying to make an universal bubble sort function. It allow to user to write its own compare and swap function. I implemented a swap and compare function for int type, but when I run the code for next array: {3, 5, 8, 9, 1, 2, 4, 7, 6, 0} , I get: 0 0 84214528 2312 1 2 4 7 6 0. Why this is happening? #include <stdio.h> #include <stdlib.h> #define true 1 #define false 0 int compInt(void *a, void *b) // FUNCTION FOR COMPARE INT { if (*(int*)(a) > *(int*)(b)) { return false; } // IF FIRST INT > SECOND INT (WRONG ORDER) RETURN FALSE return true; // RIGHT ORDER -> RETURN TRUE } I think the

C++: Mean Median and Mode

♀尐吖头ヾ 提交于 2019-12-01 10:34:19
I've recently created a C++ program to find the mean median and mode of an array of values. I realize this would be much better to do within a class. However, my function to generate the mean is not spitting out the right number, although I'm pretty certain the logic is fine. Also, I was able to modify a snipbit from something I found online to create a function that generates the mode, or at least the 1st most occurring values it can find, that I was able to implement. However, I am not 100% sure of how to wrap my head around what is actually happening within the function. A better

C++: Mean Median and Mode

拜拜、爱过 提交于 2019-12-01 07:34:18
问题 I've recently created a C++ program to find the mean median and mode of an array of values. I realize this would be much better to do within a class. However, my function to generate the mean is not spitting out the right number, although I'm pretty certain the logic is fine. Also, I was able to modify a snipbit from something I found online to create a function that generates the mode, or at least the 1st most occurring values it can find, that I was able to implement. However, I am not 100%

How do I make this function take arbitrary strings?

本小妞迷上赌 提交于 2019-11-30 21:15:29
问题 So basically, right now, this function can only take 9 words with 10 characters each. How do i make it so that it can take an arbitrary amount of words and characters and sort them accordingly in alphabetical order? int sortText(){ char name[10][9], tname[10][9], temp[10]; int i, j, n; printf("Enter the amount of words you want to sort (max 9):"); scanf("%d", &n); printf("Enter %d words: ",n); for (i = 0; i < n; i++) { scanf("%s", name[i]); strcpy(tname[i], name[i]); } for (i = 0; i < n - 1 ;