insertion-sort

Fastest strategy to form and sort an array of positive integers

痴心易碎 提交于 2019-12-01 20:41:47
In Java, what is faster: to create, fill in and then sort an array of ints like below int[] a = int[1000]; for (int i = 0; i < a.length; i++) { // not sure about the syntax a[i] = Maths.rand(1, 500) // generate some random positive number less than 500 } a.sort(); // (which algorithm is best?) or insert-sort on the fly int[] a = int[1000]; for (int i = 0; i < a.length; i++) { // not sure about the syntax int v = Maths.rand(1, 500) // generate some random positive number less than 500 int p = findPosition(a, v); // where to insert if (a[p] == 0) a[p] = v; else { shift a by 1 to the right a[p] =

Removing Duplicates Inside Insertion Sort

回眸只為那壹抹淺笑 提交于 2019-12-01 00:37:40
I am basically dealing with the following problem where i am trying to alter the insert sort so that it can also delete duplicates it counters. The following is the insert sort. public void insertSort() { for (int i = 1; i < nElems; i++) { int temp = a[i]; int j = i; while (j > 0 && temp <= a[j - 1]) { a[j] = a[j - 1]; j--; } a[j] = temp; } } I am not quire sure if i have understood the approach correctly. IF i am understanding this correctly(please tell me if i am wrong or not) the approach suggests that i should iterate through the entire array before the inner while loop begins and label

For inputs of size n, for which values of n does insertion-sort beat merge-sort? [closed]

风格不统一 提交于 2019-11-30 09:01:10
In the book Introduction to Algorithms (Corman), exercise 1.2-2 asks a the following question about comparing implementations of insertion sort and merge sort. For inputs of size n, insertion sort runs in 8n^2 steps while merge sort runs in 64n lg n steps; for which values of n does insertion sort beat merge sort? Although I am interested in the answer, I am more interested in how to find the answer step by step (so that I can repeat the process to compare any two given algorithms if at all possible). At first glance, this problem seems similar to something like finding the break even point in

Insertion Sort vs. Selection Sort

痞子三分冷 提交于 2019-11-29 18:35:59
I am trying to understand the differences between Insertion Sort and Selection Sort. They both seem to have two components: an unsorted list and a sorted list. They both seem to take one element from the unsorted list and put it into the sorted list at the proper place. I have seen some sites/books saying that selection sort does this by swapping one at a time while insertion sort simply finds the right spot and inserts it. However, I have seen other articles say something, saying that insertion sort also swaps. Consequently, I am confused. Is there any canonical source? Selection Sort: Given

Insertion Sort in OpenMP

自古美人都是妖i 提交于 2019-11-29 15:11:54
I'm trying to write OpenMP solution for Insertion sort but I'm having problems to make it run in parallel and give correct results :). Is there any way to make Insertion sort it run in parallel. Here is my code: void insertionsort(int *A, int num) { // clock_t start, stop; // // start=clock(); int k; #pragma omp parallel for shared(A) private(k) for(int n = 1; n < num; n++) { int key = A[n]; k = n; #pragma omp critical for(;k>0 && A[k-1]> key;k--) { A[k] = A[k-1]; } A[k] = key; } // stop=clock(); // cas = (double)(stop-start)/CLOCKS_PER_SEC; } dreamcrash You can not parallelize the Insertion

For inputs of size n, for which values of n does insertion-sort beat merge-sort? [closed]

戏子无情 提交于 2019-11-29 12:15:13
问题 In the book Introduction to Algorithms (Corman), exercise 1.2-2 asks a the following question about comparing implementations of insertion sort and merge sort. For inputs of size n, insertion sort runs in 8n^2 steps while merge sort runs in 64n lg n steps; for which values of n does insertion sort beat merge sort? Although I am interested in the answer, I am more interested in how to find the answer step by step (so that I can repeat the process to compare any two given algorithms if at all

Combining MergeSort with Insertion sort to make it more efficient

a 夏天 提交于 2019-11-29 05:23:07
So I have a MergeSort algorithm and I want to combine MergeSort with Insertion sort to reduce the overhead of merging, the question is how? I want to sort the segments using insertion sort and then merge. public class mergesorttest{ public static void main(String[]args){ int d[]= {10,2,3,4,5,6,5,4,3,5,6,7,1}; mergeSort(d,0,d.length); for(int x:d) System.out.print(x+" "); System.out.println(); } static void mergeSort(int f[],int lb, int ub){ //termination reached when a segment of size 1 reached -lb+1=ub if(lb+1<ub){ int mid = (lb+ub)/2; mergeSort(f,lb,mid); mergeSort(f,mid,ub); merge(f,lb,mid

Insertion Sort vs. Selection Sort

只愿长相守 提交于 2019-11-28 13:13:25
问题 I am trying to understand the differences between Insertion Sort and Selection Sort. They both seem to have two components: an unsorted list and a sorted list. They both seem to take one element from the unsorted list and put it into the sorted list at the proper place. I have seen some sites/books saying that selection sort does this by swapping one at a time while insertion sort simply finds the right spot and inserts it. However, I have seen other articles say something, saying that

How to optimize quicksort

江枫思渺然 提交于 2019-11-28 05:02:00
I am trying to work out an efficient quicksort algo. It works okay, but takes long time to run when the number of elements are huge, and certain sections of the array are pre-sorted. I was looking up the Wikipedia article on quicksort , and there I found this written: To make sure at most O(log N) space is used, recurse first into the smaller half of the array, and use a tail call to recurse into the other. Use insertion sort, which has a smaller constant factor and is thus faster on small arrays, for invocations on such small arrays (i.e. where the length is less than a threshold t determined

Why is Insertion sort better than Quick sort for small list of elements?

一世执手 提交于 2019-11-27 07:49:34
Isnt Insertion sort O(n^2) > Quick sort O(nlogn)...so for a small n, wont the relation be the same? Casey Robinson Big-O Notation describes the limiting behavior when n is large, also known as asymptotic behavior. This is an approximation. (See http://en.wikipedia.org/wiki/Big_O_notation ) Insertion sort is faster for small n because Quick Sort has extra overhead from the recursive function calls. Insertion sort is also more stable than Quick sort and requires less memory. This question describes some further benefits of insertion sort. ( Is there ever a good reason to use Insertion Sort? )