When count is equal to -1, you are trying to access the getcost method of drinks[-1]. I believe this will be fixed if you change "while count >= -1" to "while count > -1". This will obviously require a small amount of restructuring, for the drink to then be inserted...

This is probably because "345" > "2345" is True, because the string "345" comes lexicographically after "2345". If you want numeric sorting, try filling your original list with numbers instead of strings. array = [1534, 78675, 2345, 7861, 345, 8761, 1, 27456] i = 0 while i <= (len(array) -...

c++,arrays,sorting,insertion-sort

In print and getInput your variable i is not initialized to 0 You should initialize your i to 0 for(int i = 0; i<n;i++) { cout<<"Number? "; cin>>a[i]; } Same for the print method. Also, you should initialize your array size with a cont var. For more details const int...

c,algorithm,sorting,insertion-sort

You have a typo. A[i+i] = key; should be A[i+1] = key; Also, you have an unused variable p...

algorithm,analysis,insertion-sort

Its a boring night and I wanted to play with latex, so to expand on the comment i left into an actual answer... To summarize your question. Lets start with a basic example. Which is equivalent to This is where the form comes from. Back to a parameterized sum, we...

Your code will stop as long as you stop adding numbers to your input stream. nextInt() is looking for another integer value, and if it can't find one, it'll stop looping. Give it a try - enter in any sequence of characters that can't be interpreted as an int, and...

sorting,assembly,x86,insertion-sort

ls equ ($-s)/4 ;this is supposed to be the length of my source string This actually calculates the number of elements. mov cx, di sub cx, offset d ;here I find out with how many words ... At the second invocation of your insert proc this will set CX=4 which...

java,arrays,sorting,computer-science,insertion-sort

"but I really am crap at sorts of all kinds" What you are not doing here is helping yourself. Getting other people to do your work for you is not going to make you any better. A good place to start learning about insertion sort would be the Wikipedia...

c++,algorithm,sorting,mergesort,insertion-sort

There are two issues in your sort() routine - you're only sorting from mid to high (instead of low to high), leaving things half-unsorted. Once that's fixed, you need to stop j when it moves below low, not 0, since we're not always working from the start of the array....

java,algorithm,sorting,insertion-sort

All the for loops in your program are accessing the same index from the scores array. Change the for loop: size-- to index++ as follows for (index = 0; index <=size; index++) <br> { //Your code } ...

The problem is for (int j = loc; j < i; j++) { arr[j+1] = arr[j]; } The previous value in the array will cover the next one. It should be for (int j = i-1; j >= loc; j--) { arr[j+1] = arr[j]; } ...

algorithm,sorting,time-complexity,mergesort,insertion-sort

It's hardly possible to determine values of these constants, especially for modern processors that uses caches, pipelines, and other "performance things". Of course, you can try to find an approximation, and then you'll need Excel or any other spreadsheet. Enter your data, create chart, and then add trendline. The spreadsheet...

algorithm,sorting,insertion-sort

With normal insertion sort, you loop from start to end, and each item is moved up until it's in place. With this insertion sort, you still loop from start to end, but if the item you're on is >= the kth item, just leave it; if less, move it to...

This while ((i > 0) && (arr[i] > key)) { should be while ((i >= 0) && (arr[i] > key)) { then I get [2, 4, 7, 8] ...

javascript,html,arrays,sorting,insertion-sort

There are a number of typos causing you trouble. It looks like some variables were renamed, but you didn't rename all occurrences of them. Also, you're starting the final loop with i = i instead of i = 0. // was: pos = i - 1; // should be: j...

algorithm,computer-science,insertion-sort

Consider an n element list where the first n^(k/2) elements are decreasing [n^(k/2), n^(k/2)-1,...,1], and the remaining n-n^(k/2) elements are increasing [n^(k/2)+1, n^(k/2)+2,...,n]. Insertion sort is quadratic on the first part and linear on the second part. This is runtime Theta(n^k + n - n^(k/2)), which is Theta(n^k) as long...

java,algorithm,sorting,insertion-sort

First of all you have written a so called microbenchmark. Your results are not meaningful because you don't have a warmup phase. This is essential to let the JVM HotSpot compiler perform its runtime optimizations. Search for "java microbenchmark" to find some tools. An example is http://java-performance.info/jmh/ Just in case...

mov ecx, [ebp+12] ;put len in ecx, our loop variab This only moves the address of LEN into ECX not its value! You need to add movzx ecx, byte [ecx] You also need to define LEN=48 loop loop_1 What's this bizare use of LOOP doing here? You are mixing bytes...

java,arrays,algorithm,sorting,insertion-sort

Array passed by reference means that you're sorting in-place, with no extra space need for the output data. Hence the space complexity (what you need over and above the original data set (a)) is a constant O(1) rather than a linear O(n). In terms of the final code snippet, that...

arrays,sorting,time-complexity,mergesort,insertion-sort

Worst case complexity of Insertion sort & Quick Sort is O(n^2). Worst case complexity of merge sort is O(n*log(n)) therefore, for insertion sort & quick sort no. of steps are (10^6)^2 = 10^12 so it takes 10^12/10^8 = 10^4 or 10,000 seconds = 2.77hrs (as CPU operates 10^8 steps/sec) where...

The only problem here is that in inner loop condition j > 0 && (tmp < a[j-1]), actual comparison tmp < a[j-1] may result false, causing break of for loop, so numCompares++ which is located inside the loop will be skipped. To count comparisons precisely, small reformat is required: for(j...

c,sorting,linked-list,insertion-sort

Seems to be working, the problem could be with the print out. Example code to test the case mentioned above, converted to be C89 compatible (since I'm using Visual Studio). I would have swapped the names head and node, but I left them as is to match the original example....

c,performance,sorting,insertion-sort

There is already a function that does this, and it's built in in the C standard library: qsort. You just have to provide suitable comparison function. This function has to return -1 if the item taken as a left argument should be put earlier in the desired order, 1 if...

java,arrays,string,insertion-sort

You are really close to the solution You should compare temp vs a[in-1] instead of a[in] vs a[in+1] Also you need to check the in is always positive on the while loop in order to avoid IndexOutOfBoundsException while(in > 0 && temp.compareTo(a[in-1]) < 0) { ...

java,string,sorting,insertion-sort

First, this... for (i = 1; i < cities.size()-1; i++) should probably be... for (i = 1; i < cities.size(); i++) Second, this... j = i; should probably be... int j = i - 1; Third, this... while (cities.get(j - 1).compareToIgnoreCase(v) > 0 && j >= 2) { is a...

java,sorting,insertion-sort,code-complexity

Without further information about the underlying data, the best time complexity you can achieve with sorting algorithms is O(n log n) (n being the number of elements). Sorting algorithms like insertion sort, bubble sort, selection sort, etc., all have a time complexity of O(n²) due to their double loops. In...

The problem is in your while loop. It should be like this: void insertion_sort(int *array) { int j, i, key; for (j = 1; j < 6; j++) { key = array[j]; i = j - 1; while (i >= 0 && array[i] > key) { array[i+1] = array[i]; i...

With an array you can examine and assign elements using the [] operator. With a List you need to use get and set methods. So, where you want to assign an object to a position in the list, you use: klanten.set(index, object); and where you want to get an object...

java,sorting,insertion-sort,selection-sort

Your selection sort method is incorrect. You need an inner loop. public void reorderSelection() { int min = selectionCount; int temp = 0; for( int scan = selectionCount ; scan < selection.length - 1 ; scan++ ) { min = scan; for(int i = scan + 1; i < selection.length...

boolean,pascal,long-integer,bubble-sort,insertion-sort

In Pascal, boolean operators and and or have higher precedence than the comparison operators >, =, etc. So in the expression: while j > 0 and A[j] > key do Given that and has higher precedence, Pascal sees this as: while (j > (0 and A[j])) > key do 0...

java,sorting,compare,doubly-linked-list,insertion-sort

Use Double.compare(double d1, double d2) to compare doubles if you are using JDK 7 or later. Sorting looks really messed up. Try using the following approach: Node root; Comparator comp; Node current = root; while (current.next != null) { E data = current.data; Node i = current; while (i.previous...

java,doubly-linked-list,insertion-sort

You have a bug in insertion of n.getProb() <= curr.getProb() In your code n.setNext(curr); n.setPrev(curr.getPrev()); curr.setPrev(n); You set next of n to curr -- ok set prev of n to currPrev -- ok set prev of curr to n -- error, you missed link to prev of curr and it...

algorithm,sorting,recursion,insertion-sort

Before discussing the implementation, let's explain what this function does: it does not sort the entire array A, but only its initial n elements. You can pass the length of the array for n to sort the whole thing, but the fact that you pass the length separately is essential...

Your program should work perfectly fine. The issue here is, you defined array of 40 elements which gets initialized by default with 0, post that you overwrite the numbers that user enters and at the end post sorting you are just printing n elements in ascending order which will print...

c++,while-loop,mergesort,insertion-sort

The conditional in while (insertionSortTime < mergeSortTime) is false in the first iteration when both insertionSortTime and mergeSortTime are set to zero. That explains why the loop never got executed. Perhaps you meant to use: while (insertionSortTime <= mergeSortTime) ...