insertion-sort

C++ Alphabetical Insertion Sort

夙愿已清 提交于 2020-01-06 08:13:26
问题 We are doing a project involving storing and comparing various cities. We have become stuck after adding a new city into the database, it goes to the bottom of the list - we want it to go into the database, sorted alphabetically. As only one value can be added at a time, all the other entries will already be alphabetical. Below is the relevant code to this. The problem area is the // Insertion Sort when adding // out bit. The error codes are: [BCC32 Error] File1.cpp(250): E2294 Structure

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

旧时模样 提交于 2020-01-01 03:37:15
问题 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)

Using insertion sort on a singly linked list

守給你的承諾、 提交于 2020-01-01 00:48:36
问题 So I have an assignment where I'm giving a random list of number and I need to sort them using insertion sort. I must use a singly linked list. I looked around at other posts but none seem to help. I get what insertion sort is but I just don't know how to write it in code. Node* insertion_sort(Node* head) { Node* temp = head_ptr; while((head->n < temp->n) && (temp != NULL)) temp = temp->next; head->next = temp->next; temp->next = head; head->prev = temp; } I dont know if this is right or what

Running Time For Insertion Sort

↘锁芯ラ 提交于 2019-12-31 04:33:09
问题 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

Insertion Sort - How to accept input and print the sorted array

空扰寡人 提交于 2019-12-31 04:18:07
问题 I was trying to do a Insertion Sort Program that accepts any Data Type (Int, Double, String) then print's the sorted array. I know that my code work's but i can't figure out the real problem. import java.util.*; public class MyInsertionSort { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter anything you want"); String insertionSort = in.nextLine(); int num=Integer.parseInt(insertionSort); String array[] = new String [num]; for (int i = 0; i

Removing Duplicates Inside Insertion Sort

浪尽此生 提交于 2019-12-30 07:26:47
问题 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

How to optimize quicksort

淺唱寂寞╮ 提交于 2019-12-28 10:05:11
问题 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

32bit assembly - insertion sort doesn't work properly

孤人 提交于 2019-12-25 06:48:05
问题 My task here is to add a code that sorts the array with insertion sort. 'printf' function prints a string printArray prints the array For some reason the array doesn't get sorted, and i cant find the reason why. Help will be appreciated. main: push MSG ; print welcome message call printf add esp,4 ; clean the stack call printArray ;print the unsorted array ;;;;;;;;;;add code here;;;;;;;;;; mov eax,1 loop1: mov ebx, array add ebx, eax loop2: mov esi, ebx dec esi mov esi, [esi] ;esi holds the

How to do an insertion sort on a list of dictionaries in python?

北城余情 提交于 2019-12-24 12:40:31
问题 I have a list of dictionaries with various keys, all of which are integers, and I need to write a function which uses insertion sort to sort them by the specific key. def insertionsort(alldata, key): for i in alldata : temp = alldata[i] j = i while j > 0 and alldata[i['key']] < alldata[j - 1['key']]: # no idea how to put this alldata[j] = alldata[j-1] alldata[j] = temp 回答1: i['key'] looks like mistake. You aren't using your key variable here. Try alldata[i][key] < alldata[j - 1][key] as a

Insertion Sort on an array of strings in C#

拜拜、爱过 提交于 2019-12-23 17:53:24
问题 If I have an array of strings, such as string[] names = {"John Doe", "Doe John", "Another Name", "Name Another"}; How do I sort this array, using insertion sort? Wikipedia has some examples: https://en.wikibooks.org/wiki/Algorithm_implementation/Sorting/Insertion_sort#C.23 static void InsertSort(IComparable[] array) { int i, j; for (i = 1; i < array.Length; i++) { IComparable value = array[i]; j = i - 1; while ((j >= 0) && (array[j].CompareTo(value) > 0)) { array[j + 1] = array[j]; j--; }