insertion-sort

read data from .txt file in c and add it to linked list

会有一股神秘感。 提交于 2020-05-08 20:08:46
问题 In main function read the data and sort according to number(ascending order using insertion sort) in a linked list. typedef struct { char * name; int no; } tele; In this part the elements of the structure ‘tele’ are to be put in a linked list. typedef struct _node{ tele data; struct _node *next; } node; Input File (First five lines): 80043 CHEBIYYAM 80131 SHUKLA 80200 GANGARAPU 85400 GAURAV 80001 MUDIT Code: #include <stdio.h> #include <stdlib.h> #include <string.h> #define row 100 // total

read data from .txt file in c and add it to linked list

回眸只為那壹抹淺笑 提交于 2020-05-08 20:06:09
问题 In main function read the data and sort according to number(ascending order using insertion sort) in a linked list. typedef struct { char * name; int no; } tele; In this part the elements of the structure ‘tele’ are to be put in a linked list. typedef struct _node{ tele data; struct _node *next; } node; Input File (First five lines): 80043 CHEBIYYAM 80131 SHUKLA 80200 GANGARAPU 85400 GAURAV 80001 MUDIT Code: #include <stdio.h> #include <stdlib.h> #include <string.h> #define row 100 // total

read data from .txt file in c and add it to linked list

折月煮酒 提交于 2020-05-08 20:01:11
问题 In main function read the data and sort according to number(ascending order using insertion sort) in a linked list. typedef struct { char * name; int no; } tele; In this part the elements of the structure ‘tele’ are to be put in a linked list. typedef struct _node{ tele data; struct _node *next; } node; Input File (First five lines): 80043 CHEBIYYAM 80131 SHUKLA 80200 GANGARAPU 85400 GAURAV 80001 MUDIT Code: #include <stdio.h> #include <stdlib.h> #include <string.h> #define row 100 // total

Incorrect Worst-case time complexity of Insertion Sort

血红的双手。 提交于 2020-01-25 08:41:07
问题 Hi I am new to algorithms and am quite fascinated by it. I am trying to figure out worst case time complexity of insertion sort and it is mentioned as O(n**2). Instead, we can have the time complexity as O(N*logN). Here is my explanation, THe insertion sort looks at the 1st element and assumes it is sorted. Next it looks at the 2nd element and compares with the predecessor sorted sublist of 1 element and inserts it based on the comparison with elements in the predecessor sorted sublist. This

Insertion sort in C using linked list

折月煮酒 提交于 2020-01-15 14:24:52
问题 I have to make a telephone directory program. The program should read names and numbers from a file. I have successfully created a linked list containing this data. Now I want to sort them alphabetically. How should I do that? 回答1: It depends on your objective. If you want to do this efficiently, stick pointers to every element into an array, and then sort the array alphabetically using an algorithm like quicksort ( qsort in C); lastly, re-create the list from the sorted array. On the other

How to count comparisons and swaps in insertion sort? (JAVA)

偶尔善良 提交于 2020-01-15 09:59:06
问题 public class Sorting { public static int numOfComps = 0, numOfSwaps = 0; public static void insertionSort(int[] array) { int unsortedValue; // The first unsorted value int scan; // Used to scan the array // The outer loop steps the index variable through // each subscript in the array, starting at 1. The portion of // the array containing element 0 by itself is already sorted. for (int index = 1; index < array.length; index++) { // The first element outside the sorted portion is // array

linkedlist insertion

懵懂的女人 提交于 2020-01-06 18:37:50
问题 Can anyone tell me why Iam running off heap space when I try to run this code on a list of 2000 elements? public static <T extends Comparable <? super T>> void insertionSort2(List<T> portion){ int i = 0; int j = 0; T value; //List <T> sorted = new LinkedList<T>(); // goes through the list for (i = 1; i < portion.size(); i++) { // takes each value of the list value = (T) portion.remove(i); // the index j takes the value of I and checks the rest of the array // from the point i j = i - 1; while

linkedlist insertion

吃可爱长大的小学妹 提交于 2020-01-06 18:37:47
问题 Can anyone tell me why Iam running off heap space when I try to run this code on a list of 2000 elements? public static <T extends Comparable <? super T>> void insertionSort2(List<T> portion){ int i = 0; int j = 0; T value; //List <T> sorted = new LinkedList<T>(); // goes through the list for (i = 1; i < portion.size(); i++) { // takes each value of the list value = (T) portion.remove(i); // the index j takes the value of I and checks the rest of the array // from the point i j = i - 1; while

Insertion sort in clojure throws StackOverFlow error

核能气质少年 提交于 2020-01-06 13:58:05
问题 (defn insert [s k] (let [spl (split-with #(< % k) s)] (concat (first spl) (list k) (last spl)))) (defn insert-sort [s] (reduce (fn [s k] (insert s k)) '() s)) (insert-sort (reverse (range 5000))) throws a stack over flow error. What am I doing wrong here? 回答1: Same issue as with Recursive function causing a stack overflow. Concat builds up a bunch of nested lazy sequences like (concat (concat (concat ...))) without doing any actual work , and then when you force the first element all the