insertion-sort

iOS: How to find insertion position in sorted NSMutableArray

流过昼夜 提交于 2019-12-04 03:28:20
I have an NSMutableArray of sorted objects, which are displayed in a UITableView. I want to insert a new object into the array and update the table view - which requires the index of the newly inserted object. I can't find any system message to tell me the correct insertion index into the array which I need to update the table view. The best I can find is: add new object sort using old copy of array, find the location of the new object (which requires searching) or write my own search for insertion position Surely, there must be a message to find the insertion position in a sorted array? Or am

when is insertion sort faster than merge sort?

大城市里の小女人 提交于 2019-12-03 11:21:10
问题 For a homework problem, I was told that insertion sort runs at 8n^2 and that merge sort runs at 64(n(lg n)). As part of the solution I was given, it said that insertion sort was faster than merge sort as long as n <= 43, but the teacher's answer doesn't really explain why or how he arrived at 43. Can anyone explain this? I'm not that great with figuring out run times so I'm trying to get a better understanding. And yes, I tried asking the teacher, but I was still confused. Any help would be

Can you formulate the insertion sort as a monoid in Clojure?

旧城冷巷雨未停 提交于 2019-12-03 08:58:42
This is the code for an insertion sort in Clojure: (defn in-sort! [data] (letfn [(insert ([raw x](insert [] raw x)) ([sorted [y & raw] x] (if (nil? y) (conj sorted x) (if (<= x y ) (concat sorted [x,y] raw) (recur (conj sorted y) raw x )))))] (reduce insert [] data))) ;Usage:(in-sort! [6,8,5,9,3,2,1,4,7]) ;Returns: [1 2 3 4 5 6 7 8 9] This is the insertion sort formulated as a monoid in Haskell: newtype OL x = OL [x] instance Ord x => Monoid (OL x) where mempty = OL [] mappend (OL xs) (OL ys) = OL (merge xs ys) where merge [] ys = ys merge xs [] = xs merge xs@(x : xs') ys@(y : ys') | x <= y =

Why is insertion sort Θ(n^2) in the average case?

三世轮回 提交于 2019-12-03 01:31:04
问题 Insertion sort has a runtime that is Ω(n) (when the input is sorted) and O(n 2 ) (when the input is reverse sorted). On average, it runs in Θ(n 2 ) time. Why is this? Why isn't the average case closer to O(n log n), for example? 回答1: To answer this question, let's first determine how we can evaluate the runtime of insertion sort. If we can find a nice mathematical expression for the runtime, we can then manipulate that expression to determine the average runtime. The key observation we need

Insertion Sorting c#

☆樱花仙子☆ 提交于 2019-12-02 23:23:06
问题 Can you guys please help me with basic insertion sorting in C#. I have a list of names and city of residence in a array and need to sort this array by comparing the city of residence. List has to be sorted in alphabetical order. Comparator has been set up and works I'm just kinda lost with the insertion sorter programming as this is the first time we are doing that method of sorting. Here's what I've tried so far: public void InsertionSort() { for (int i = 0; i < Count; i++) { Student cur =

Why is insertion sort Θ(n^2) in the average case?

雨燕双飞 提交于 2019-12-02 14:55:59
Insertion sort has a runtime that is Ω(n) (when the input is sorted) and O(n 2 ) (when the input is reverse sorted). On average, it runs in Θ(n 2 ) time. Why is this? Why isn't the average case closer to O(n log n), for example? To answer this question, let's first determine how we can evaluate the runtime of insertion sort. If we can find a nice mathematical expression for the runtime, we can then manipulate that expression to determine the average runtime. The key observation we need to have is that the runtime of insertion sort is closely related to the number of inversions in the input

Insertion Sorting c#

徘徊边缘 提交于 2019-12-02 14:33:13
Can you guys please help me with basic insertion sorting in C#. I have a list of names and city of residence in a array and need to sort this array by comparing the city of residence. List has to be sorted in alphabetical order. Comparator has been set up and works I'm just kinda lost with the insertion sorter programming as this is the first time we are doing that method of sorting. Here's what I've tried so far: public void InsertionSort() { for (int i = 0; i < Count; i++) { Student cur = Attendees[i]; for (int j = 0; j < Count; j++) { Student Sel = Attendees[j]; if (cur.CompareTo(Sel) < 0)

Number of comparisons in insertion sort

核能气质少年 提交于 2019-12-02 12:06:53
In this program, I want to calculate the number of data comparisons in the insertion sort, but my code is not working as I expected. def insertionSort(list): numOfComp = 0 for i in range(1,len(list)): value = list[i] j = i - 1 while j>=0: if value < list[j]: list[j+1] = list[j] list[j] = value j = j - 1 numOfComp += 1 if value >= list[j]: numOfComp += 1 j = j - 1 else: break print("Number of data comparisons:",numOfComp) print("Sorted list:",list) the problem is if value >= list[j]: numOfComp += 1 j = j - 1 If value >= list[j] you can and should simply exit your while loop and stop further

Running Time For Insertion Sort

被刻印的时光 ゝ 提交于 2019-12-02 08:08:00
for (int p=1; p < a.size(); p++) { int tmp = a[p]; for(j=p; j>0 && tmp < a[j-1]; j--) { a[j] = a[j-1]; } a[j] = tmp; } I'm having trouble figuring out the worse case for Insertion sort. So, the array given is in descending order, and we want to sort it in ascending order. The outer loop goes through the array. So, it runs (n times). O(n) int tmp=a[p] ---- This statement gets executed n times. O(n) The inner loop get executed (1+2+3+4+5+6+.... +n-1) times. O(n^2) a[j]= tmp -------- This statement get executed n times. O(n) I'm not sure what to do after to find the running time for Insertion

Implementing a binary insertion sort using binary search in Java

泪湿孤枕 提交于 2019-12-02 01:56:38
I'm having trouble combining these two algorithms together. I've been asked to modify Binary Search to return the index that an element should be inserted into an array. I've been then asked to implement a Binary Insertion Sort that uses my Binary Search to sort an array of randomly generated ints . My Binary Search works the way it's supposed to, returning the correct index whenever I test it alone. I wrote out Binary Insertion Sort to get a feel for how it works, and got that to work as well. As soon as I combine the two together, it breaks. I know I'm implementing them incorrectly together,