bubble-sort

I want an efficient sorting algorithm to sort an array

孤者浪人 提交于 2019-12-12 02:49:35
问题 for (int i = 1; i < data.Count; i++) { int j = i; while (j > 0) { if (numarray[j - 1] > numarray[j]) { int temp = numarray[j - 1]; numarray[j - 1] = numarray[j]; numarray[j] = temp; j--; } else break; } } Can someone help me identify what is the sorting algorithm of the above code? I know that bubble sort is not very efficient. If I am to use insertion sort algorithm instead, how can I improve the above code. Thankyou! 回答1: Just do this: Array.Sort(data); 回答2: the most efficient way should be

Bubble sort Linked list C++

这一生的挚爱 提交于 2019-12-12 01:16:36
问题 I'm having problems with this code. I'm pretty sure it's in the swapping. The line: curr->Data() = nextEl.Data() gives me the following error: "expression must be a modifiable lvalue" Any help is appreciated. Thank you in advance. Here is the code for my bubble-sort algorithm: class Node { private: int data; Node* next; public: Node() {}; void Set(int d) { data = d;}; void NextNum(Node* n) { next = n;}; int Data() {return data;}; Node* Next() {return next;}; }; class LinkedList { Node *head;

How Sort an array of objects regardless of object variable type

丶灬走出姿态 提交于 2019-12-11 21:15:18
问题 I have an array of Record objects and each record object has 5 fields (first name, last name, GPA, ID number,and email). I want to sort the array based on any of the variables attributed to the object. My professor says there is a way to use one function that will sort it regardless of the variable type that is passed through. But I can't figure out a way to have one function that can sort any of those 5 variables and 3 different variable types. Meaning I can't just copy paste my sort

Bubble Sort in Python 3

时间秒杀一切 提交于 2019-12-11 15:39:00
问题 Write a bubble sort program in Python 3. A bubble sort is an algorithm that will sort a list of values into order. I am trying to get this result towards the end. Original List: 4, 9, 74, 0, 9, 8, 28, 1 Sorted List: 0, 1, 4, 8, 9, 9, 28, 74 Number of Passes: 6 How can I accomplish it? import sys def bubblesort(mylist): changes = passes = 0 last = len(mylist) swapped = True print("Original List: ", ','.join(map(str, mylist)) ) while swapped: swapped = False for j in range(1, last): if mylist[j

bubble sort comparison count is always the same

吃可爱长大的小学妹 提交于 2019-12-11 15:12:23
问题 I am having trouble understanding how comparisons work and how many are made in bubble sort(or any sort for that matter). In my example code for bubble sort: public class BS { public static void main (String[] args) { int[] Array = {5,1,4,2,8}; System.out.println("Array Before Bubble Sort:"); for (int element : Array) System.out.print(element + " "); bubbleSort(Array); System.out.println("Array After Bubble Sort:"); for (int element : Array) System.out.print(element + " "); System.out.print("

bubble sort. C++

自作多情 提交于 2019-12-11 14:49:21
问题 What is wrong with my bubble sort code and how do I get it to write out after sorting (before the Linesearch). I've used code based on the only example I could find in the book. Searched around the net for some guide on how to sort an array list by age but I could not find one (at least, not one that wasn't too advanced for me). So I'm back with another piece of code that probably will make your eyes bleed ^^ Sorry. #include <iostream> #include <string> using namespace std; class person {

Hackerearth bubbleSort

只愿长相守 提交于 2019-12-11 14:08:41
问题 In Hackerearth i tried solving bubble sort swap counting. and my output always different from correct output.for example; my output is 2475 and correct output is 2788 #include <iostream> using namespace std; int main() { int *A,tm,times=0; cin >> tm; A = new int[tm]; for(int i = 0; i<tm;i++) {cin >> A[i];} int temp; for(int i = 0; i<tm;i++){ for(int j = 0; j < tm-i-1;j++){ if(A[j] > A[j+1]){ times++;; temp = A[j]; A[j] = A[j+1]; A[j] = temp; } } } cout << times; return 0; } Am i doing

Number of swaps performed by Bubble-Sort on a given array

こ雲淡風輕ζ 提交于 2019-12-11 13:43:44
问题 I have been given an array and I'm asked to find out the number of Swaps required to sort the array using Bubble Sort Now we know that, we can find the comparisons by n(n-1)/2 but what I need is the number of actual swaps My first instinct was to use bubble sort and with each swap(), I incremented a Swap variable. But the time complexity of this is a very slow process and I'd like your help to find an optimized way to solve my dilemma P.S.: I also need to compare whether it is faster to sort

How to use a sorting algorithm in Java?

北战南征 提交于 2019-12-11 07:03:57
问题 My assignment is to ask a user for the number of strings he would like to input. Then i will prompt him to right the strings. Then i am supposed to print the stings in alphabetical order. I got most of the assignment done, just need a sorting algorithm such as Bubble sort to finish it. this is my code. import java.io.*; public class sorting { private static BufferedReader stdin=new BufferedReader(new InputStreamReader( System.in)); public static void main(String[] arguments) throws

Sorting a Singly Linked List With Pointers

限于喜欢 提交于 2019-12-11 06:55:43
问题 I am trying to sort a singly linked list using bubble sort by manipulating ONLY the pointers, no keys. The following gets stuck in the for loop and loops infinitely. I don't understand why this is. Can anybody explain to me why the end of the list is not being found? Node* sort_list(Node* head) { Node * temp; Node * curr; for(bool didSwap = true; didSwap; ) { didSwap = false; for(curr = head; curr->next != NULL; curr = curr->next) { if(curr->key > curr->next->key) { temp = curr; curr = curr-