bubble-sort

bubble sort ideone time limit exceeded

大憨熊 提交于 2019-12-08 08:12:57
问题 #include <stdio.h> int main(void) { int a[5]={1,2,0,5,4},i,j,c; for(i=0;i<5;i++) { for(j=0;j<5-i;j++){ if(a[j]>a[j+1]) { c=a[j]; a[j]=a[j+1]; a[j+1]=c; } } } for(i=0;i<5;i++) { printf("%d",a[i]); } return 0; } ideone says "Time limit exceeded time: 5 memory: 2048 signal:24" but it works fine on turbo compiler 回答1: for(j=0;j<5-i;j++){ if(a[j]>a[j+1]) a[j+1] is array out of bound access which will lead to undefined behavior 回答2: Try this for bubble sort..Machine cycle will not be hampered in

Sorting a linked list in Java

社会主义新天地 提交于 2019-12-06 18:28:12
问题 I have written a bubble sort algorithm to sort a linked list. I am a Java beginner and trying to learn data structures. I am confused why my second element is not sorted properly. EDIT class SListNode { Object item; SListNode next; SListNode(Object obj) { item = obj; next = null; } SListNode(Object obj, SListNode next) { item = obj; this.next = next; } } public class SList { private SListNode head; private SListNode temp; public void sortList() { SListNode node = head,i,j; head = node; i =

Bubble Sort Manually a Linked List in Java

戏子无情 提交于 2019-12-06 12:01:40
问题 This is my first question here. I am trying to manually sort a linked list of integers in java and I can not figure out what is wrong with my code. Any suggestions? I don't get any error, however I still have my output unordered. I tried a few different ways, but nothing worked. I appreciate if anyone can help me with that. public class Node { int data; Node nextNode; public Node(int data) { this.data = data; this.nextNode = null; } public int getData() { return this.data; } } // Node class

overloaded function with no contextual type information | cannot resolve overloaded function 'swap' based on conversion to type 'int'

可紊 提交于 2019-12-06 05:19:16
问题 I am trying to write my own bubble sort algorithm as an exercise. I do not understand the two error messages. Can anyone point out the problem with my code? // Bubble sort algorithm #include <iostream> #include <iomanip> using namespace std; void bubbleSort(int array[], int arraySize); // bubbleSort prototype int main(void) { const int arraySize = 10; int array[arraySize] = {2,3,6,5,7,8,9,3,7,4}; cout << "Unsorted: "; for(int i = 0; i < arraySize; ++i) cout << setw(5) << array[i]; cout <<

JavaScript BubbleSort, how to improve its efficiency?

泪湿孤枕 提交于 2019-12-06 05:17:59
问题 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; } } } 回答1: Before enter the inner loop, create a boolean to check if a

Bubble Sorting in Scheme

穿精又带淫゛_ 提交于 2019-12-06 04:30:37
问题 I am writing a recursive code to Bubble Sort (smallest to largest by swapping) I have a code to do the bubble sort just once (define (bubble-up L) (if (null? (cdr L)) L (if (< (car L) (cadr L)) (cons (car L) (bubble-up (cdr L))) (cons (cadr L) (bubble-up (cons (car L) (cddr L)))) ) ) if i put a list into this code, it returns the list with the largest number at the end EX.. (bubble-up ' (8 9 4 2 6 7)) -> ' (8 4 2 6 7 9) Now i am trying to write a code to do the (bubble-up L) N times (the

Bubble sort in c linked list [closed]

℡╲_俬逩灬. 提交于 2019-12-05 08:14:54
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 2 years ago . 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

insertion sort vs bubble sort vs quicksort algorithm

会有一股神秘感。 提交于 2019-12-05 07:09:06
问题 I am working on a research in the class I tested bubble sort and insertion sort and quick sort , i did the test on random numbers. The results shows that insertion sort is more quicker than bubble sort and quick sort is the most slower one. So I have the below ranking in terms of time insertion sort (the fastest) bubble sort (second score) quick sort (the slowest) Taking into account that insertion and bubble sort have a complexity of O(n2) while quick sort O(n log n) and O (n log n) should

Bubble Sort Manually a Linked List in Java

梦想与她 提交于 2019-12-04 17:23:54
This is my first question here. I am trying to manually sort a linked list of integers in java and I can not figure out what is wrong with my code. Any suggestions? I don't get any error, however I still have my output unordered. I tried a few different ways, but nothing worked. I appreciate if anyone can help me with that. public class Node { int data; Node nextNode; public Node(int data) { this.data = data; this.nextNode = null; } public int getData() { return this.data; } } // Node class public class DataLinkedList implements DataInterface { private Node head; private int size; public

overloaded function with no contextual type information | cannot resolve overloaded function 'swap' based on conversion to type 'int'

假装没事ソ 提交于 2019-12-04 11:36:11
I am trying to write my own bubble sort algorithm as an exercise. I do not understand the two error messages. Can anyone point out the problem with my code? // Bubble sort algorithm #include <iostream> #include <iomanip> using namespace std; void bubbleSort(int array[], int arraySize); // bubbleSort prototype int main(void) { const int arraySize = 10; int array[arraySize] = {2,3,6,5,7,8,9,3,7,4}; cout << "Unsorted: "; for(int i = 0; i < arraySize; ++i) cout << setw(5) << array[i]; cout << "Sorted: " << bubbleSort(array, arraySize); } void bubbleSort(int array[], int arraySize) { const int max