insertion-sort

Sorting an array in openmp

落爺英雄遲暮 提交于 2019-12-14 00:40:22
问题 I have an array of 100 elements that needs to be sorted with insertion sort using OpenMP. When I parallelize my sort it does not give correct values. Can some one help me void insertionSort(int a[]) { int i, j, k; #pragma omp parallel for private(i) for(i = 0; i < 100; i++) { k = a[i]; for (j = i; j > 0 && a[j-1] > k; j--) #pragma omp critical a[j] = a[j-1]; a[j] = k; } } 回答1: Variables "j" and "k" need to be private on the parallel region. Otherwise you have a data race condition. 回答2:

Insertion sort using a list STL recursively

有些话、适合烂在心里 提交于 2019-12-13 17:32:29
问题 So I am trying to modify this code of vectors into lists. I understand vectors but fairly new to lists. This is what I have tried so Far how can I fix this Please let me Know. Here is the original vector code: void insertion_sort(std::vector <int> &num) { int i, j, key; bool insertionNeeded = false; for (j = 1; j < num.size(); j++) { key = num[j]; insertionNeeded = false; for (i = j - 1; i >= 0; i--) { // larger values move right if (key < num[i]) { num[i + 1] = num[i]; insertionNeeded = true

C - How do you call the first element in a linked list?

情到浓时终转凉″ 提交于 2019-12-13 07:46:57
问题 I am trying to get a linked list to sort, then be able to display it. The problem with my code is, I can display it before sorting, but after sorting, it won't display, it will crash. I think it has to do with the "top" variable, because through debugging, it doesn't contain anything. How can I call the first element in the linked list and use that to display them all? I am just really confused. Below is only the display and sort functions. //Sort and display all employees void displayAllEmps

Trying to understand insertion sort algorithm

被刻印的时光 ゝ 提交于 2019-12-12 08:04:41
问题 I'm reading some books on Python, data structures, and analysis and design of algorithms. I want to really understand the in's and out's of coding, and become an efficient programmer. It's difficult to ask the book to clarify, hence my question on stackoverflow. I'm really finding Algorithms and recursion challenging ... I posted some code (insertion sort) below that I'm trying to understand exactly what's happening. I understand, generally, what is supposed to happen, but I'm not really

Efficency of Insertion Sort vs Bubble sort vs Selection sort?

こ雲淡風輕ζ 提交于 2019-12-12 07:24:48
问题 I have written down that Insertion Sort is faster than Selection Sort, which is faster than Bubble Sort, and that their running time for all 3 are O(n^2), but what can I say to compare them with each other? 回答1: You can compare sorting algorithms against the following criteria: Time Complexity (Big-O notation). You should note that best-case, worst-case and average run-time can have different time complexity. For example best-case for Bubble Sort is only O(n), making it faster than Selection

SMLNJ Insertion Sort Operator and Operand dont agree error

我的梦境 提交于 2019-12-12 04:03:02
问题 Im making an insertion sort code in SML, here it is fun compare(x:real, y:real, F) = F(x, y); fun isEqual(x:real, y:real) = ((x <= y) andalso (x >= y)); fun rinsert(x: real, [], F) = [x] |rinsert(x, (y::ys), F) = if isEqual(x, y) then rinsert (x, ys, F) else if compare(x, y, F) then x::y::ys else y::(rinsert (x, ys, F)); fun rinsort([], F) = [] |rinsort(x::xs, F) = rinsert(x, (rinsort(xs, F), F)); However, on running it i get this error val isEqual = fn : real * real -> bool val rinsert = fn

I want an efficient sorting algorithm to sort an array

孤者浪人 提交于 2019-12-12 02:49:35
问题 for (int i = 1; i < data.Count; i++) { int j = i; while (j > 0) { if (numarray[j - 1] > numarray[j]) { int temp = numarray[j - 1]; numarray[j - 1] = numarray[j]; numarray[j] = temp; j--; } else break; } } Can someone help me identify what is the sorting algorithm of the above code? I know that bubble sort is not very efficient. If I am to use insertion sort algorithm instead, how can I improve the above code. Thankyou! 回答1: Just do this: Array.Sort(data); 回答2: the most efficient way should be

Is my code a correct implementation of insertion sort?

隐身守侯 提交于 2019-12-12 01:24:42
问题 This code sorts correctly. Is this an insertion sort? import java.util.Scanner; public class InsertionSort { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter the number of elements: "); int count; count = sc.nextInt(); int[] a = new int[count]; System.out.println("Enter elements: "); for(int i = 0 ; i<count;i++){ a[i] = sc.nextInt(); } int j,temp; System.out.println("aftr insertion sort :"); for(int i = 1 ; i<count;i++){ j=i; while(j>0 &&

Error with c insertion sort

戏子无情 提交于 2019-12-12 00:58:55
问题 I am making a c insertion sort and it works fine except that after the sort the first number is always a weird negative number and the program errors out. #include <stdio.h> #include <stdlib.h> #include <time.h> void insertionSort(int list[], int last){ int hold; int walker; int current; int count; count = 0; for (current = 1; current <= last; current++){ hold = list[current]; for (walker = current - 1; walker >= 0 && hold < list[walker]; walker--){ list[walker + 1] = list[walker]; } list

Insertion sort linked list c++

梦想与她 提交于 2019-12-12 00:09:41
问题 I'm trying to sort a filled linked list with random numbers. The function I have made doesnt work as it should. I can't see what is wrong, its not sorting the numbers properly. void linked_list::SortList() { if(is_empty()) { return; } for(node_t *it =head; it!=tail; it = it->next) { int valToIns = it->value; node_t *holePos = it; while(holePos->prev && valToIns < it->prev->value) { holePos->value = holePos->prev->value; holePos = holePos->prev; } holePos->value = valToIns; } } 回答1: You're