bubble-sort

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

£可爱£侵袭症+ 提交于 2019-12-20 06:38:30
问题 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

Bubble sort universal implementation in C

耗尽温柔 提交于 2019-12-20 02:26:22
问题 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

BubbleSort Implementation

白昼怎懂夜的黑 提交于 2019-12-18 05:26:08
问题 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

bubble sort implementation on linked lists

ぐ巨炮叔叔 提交于 2019-12-17 20:33:38
问题 I have to implement a BubbleSort algorithm on a Linked List instead of an array. I'm new to java so I don't really know how to put it in code. But I gave it a try and here's what I got: SinglyNode.java public class SinglyNode { public Object names; public SinglyNode next; public SinglyNode (Object name1) { names = name1; } public SinglyNode (Object name2, SinglyNode next1) { names = name2; next = next1; } Object getObject() { return names; } SinglyNode getNext() { return next; } void

How to sort a linked list using bubble-sort?

半城伤御伤魂 提交于 2019-12-17 10:46:50
问题 I am trying to use bubble-sort in order to sort a linked list. I use curr and trail in order to traverse thru the list. curr is supposed to be one step ahead of trail always. This is my code so far: void linked_list::sort () { int i,j=0; int counter=0; node *curr=head; node *trail=head; node *temp=NULL; while (curr !=NULL) { curr=curr->next; //couting the number of items I have in my list. counter++; //this works fine. } curr=head->next; // reseting the curr value for the 2nd position. for (i

c++ sort with structs

痞子三分冷 提交于 2019-12-17 04:34:11
问题 I am having a hard time with this problem which requires a sort of customer names, customer ids, and finally amount due. I have the whole program figured, but cannot figure out the last prototype needed to do the sorting. i have a struct called Customers, and i will provide the int main() part also. I just need any help to gt started on the prototype SortData(). struct Customers { string Name; string Id; float OrderAmount; float Tax; float AmountDue; }; const int MAX_CUSTOMERS = 1000; bool

program stops after reading procedure (delphi)

帅比萌擦擦* 提交于 2019-12-14 03:14:26
问题 My program stops to read anymore lines and ends the program after this procedure like its 'end.' after it (but its not) : Procedure BubbleSort; var i, j : integer; begin for i := 0 to count - 1 do begin for j := count - 1 downto i do if (together[j] > together[j - 1]) then Swap(together[j - 1], together[j]); end; end; 回答1: I guess the problem is the out of bounds array access. You access index -1. Avoid this by changing the outer loop to: for i := 1 to count - 1 do begin I suggest that you

Sort 2D matrix in C

有些话、适合烂在心里 提交于 2019-12-13 23:21:33
问题 I am new to programming in C, and have but one problem that I can't seem to figure out on my own. I have created a 2-dimensional matrix and need to sort all 100 randomly generated numbers in the matrix using bubble sort. I also need to save the output for later use in my program. Has anybody got any idea? #include <stdio.h> #include <stdlib.h> #include <time.h> int main() { int i, j, matrix[10][10]; // Generate random numbers in a matrix printf("Numbers generated:\n"); srand((int)time(NULL));

How to merge 2 arrays based on a key name and sort based on the merged value?

允我心安 提交于 2019-12-13 15:19:28
问题 assume I have two list const listA = [{"apple":100}, {"banana": 50}, {"pearl": 10}, {"cherry": 5}, {"kiwi": 3}] const listB = [{"peach": 30}, {"apple": 15}, {"kiwi": 10}, {"mango": 5}] The question is how do I merge two list into one stack up the same item with number increment and sort by the qty? I mean the final result should be -> const listMerged = [{"apple":115}, {"banana": 50} , {"peach": 30}, {"kiwi": 13}, {"pearl": 10}, {"cherry": 5}, {"mango": 5}] I know it is gonna be something

Bubble sort string array c#

安稳与你 提交于 2019-12-13 09:42:49
问题 I have this project where I have to sort some txt files, most of them are numbers but one of them is a collection of months. I have the code that sorts the others but not the file containing the months. So i need this code to be changed so I can sort a string array any advice would be brilliant thank you! public void SortArray(decimal[] numbers) { bool swap; decimal temp; do { swap = false; for(int index = 0; index < (numbers.Length - 1); index ++) { if ( numbers[index] > numbers[index+1]) {