mergesort

Why Merge Operation in Merge Sort is O(n)?

≯℡__Kan透↙ 提交于 2019-12-02 07:29:01
问题 For merge-sort divide and conquer operations, how much time is required in bottom up merging phase? My instructor says that it is be linear, hence it will be O(n) . But I didn't get it. How will it be linear? How will merging operation be linear O(n) ? 回答1: Merge operation of two arrays, is scanning arrays and picking the lowest/highest of two. so you have a: [1, 3, 6, 7] b: [4, 5, 7, 8] you compare like this (sort of pseudo code) indexA = 0; indexB = 0; auxilaryArray = []; indexAux = 0;

Javascript merge sort visualisation

风流意气都作罢 提交于 2019-12-02 05:01:58
I have managed to get a merge sort working in p5.js to sort different length lines but can not figure out how to actually show them being sorted. I.e show them unsorted and then update their position as they are being sorted. I'm not sure if there is an easy way to do this with the way my code is currently written or if I need to break the sorting function up and re draw it after each stage? var values = []; var numLines = 500; function setup() { createCanvas(900, 600); colorMode(HSB, height); for (i = 0; i < numLines; i++) { values[i] = (round(random(height))); } values = mergeSort(values);

Why Merge Operation in Merge Sort is O(n)?

江枫思渺然 提交于 2019-12-02 03:12:43
For merge-sort divide and conquer operations, how much time is required in bottom up merging phase? My instructor says that it is be linear, hence it will be O(n) . But I didn't get it. How will it be linear? How will merging operation be linear O(n) ? Merge operation of two arrays, is scanning arrays and picking the lowest/highest of two. so you have a: [1, 3, 6, 7] b: [4, 5, 7, 8] you compare like this (sort of pseudo code) indexA = 0; indexB = 0; auxilaryArray = []; indexAux = 0; while true if indexA > len(a)-1 or indexb > len(b) -1 break # you are cherry picking one from the two array

How to speed up external merge sort in Java

我的未来我决定 提交于 2019-12-02 02:34:41
问题 I am writing code for the external merge sort. The idea is that the input files contain too many numbers to be stored in an array so you read some of it and put it into files to be stored. Here's my code. While it runs fast, it is not fast enough. I was wondering if you can think of any improvements I can make on the code. Note that at first, I sort every 1m integers together so I skip iterations of the merging algorithm. import java.io.BufferedInputStream; import java.io.BufferedOutputStream

Counting Inversions Using Merge Sort [closed]

喜夏-厌秋 提交于 2019-12-02 01:35:39
I have made a merge sort program in Python and it is running perfectly but I have modified it to count the number of inversions involved and now it is giving me an error : Here's my code: def merge_list(left,right,c): result=[] i,j=0,0 while i < len(left) and j < len(right): if left[i] < right[j]: result.append(left[i]) print "Left result",result i=i+1 elif left[i] > right[j]: result.append(right[j]) print "Right result",result j=j+1 if right[j] < left[i] and i<j: c=c+1 result=result+left[i:] result=result+right[j:] print "Inversions: ",c return result,c def sort_and_count(lis,count): if len

How to implement merge sort from “The Introduction to Algorithms” by Cormen and Co

雨燕双飞 提交于 2019-12-01 16:16:21
I'm learning algorithms from Cormen and Co. and I have problem with implementation of merge sort from their pseudocode. I compiled it by: $ gcc -Wall -g merge_sort.c I have a problem because for numbers: 2 4 5 7 1 2 3 6 The result is: 1 2 2 3 3 4 5 5 I tried to read carefully the pseudo code but this does not help me. I want to know what I'm doing wrong. Below is my code: #include <stdio.h> #define SIZE 8 void merge(int *array_of_integers, int p, int q, int r) { int n1 = q - p + 1; int n2 = r - q; int i, j, k; int left_array[n1 + 1]; int right_array[n2 + 1]; for (i = 0; i < n1; i++) left_array

complexity of mergesort with linked list

元气小坏坏 提交于 2019-12-01 12:12:20
i have code for mergesort using linked list,it works fine,my question what is complexity of this algorithm?is it O(nlog(n))?also is it stable?i am interested because as i know mergesort is stable,what about using linked list?if we have elements with some equal with each-other,does this code preserve orders of elements?thanks a lot #include<stdio.h> #include <stdlib.h> struct node { int number; struct node *next; }; struct node *addnode(int number,struct node *next); struct node*mergesort(struct node *head); struct node *merge(struct node *one,struct node *two); int main(void){ struct node

Merge sort for f sharp

。_饼干妹妹 提交于 2019-12-01 08:41:59
This is my code, when I enter a very large number I get stack overflow error does anyone know why? When i enter a very large number i get that error and im not really sure what is causing it, it is only with large numbers small ones work fine..... // // merge two sorted lists into one: // let rec merge L1 L2 = if L1 = [] && L2 = [] then [] else if L1 = [] then L2 else if L2 = [] then L1 else if L1.Head <= L2.Head then L1.Head :: merge L1.Tail L2 else L2.Head :: merge L1 L2.Tail // // mergesort: // let rec mergesort L = match L with | [] -> [] | E::[] -> L | _ -> let mid = List.length L / 2 let

Java: How to sort custom type ArrayList

陌路散爱 提交于 2019-12-01 08:30:16
I have a custom type Position(x,y,z) ,now I create a ArrayList<Position> , i want to sort this array ordered by the value of z, from small to bigger,how can i do that using Collections.sort or is there any other efficient sorting method? When I try to use public class PositionComparator implements Comparator<Position> { @Override public int compare(Position o1, Position o2) { // TODO Auto-generated method stub return o1.height().compareTo(o2.height()); } } get an error Cannot invoke compareTo(double) on the primitive type double try Collections.sort(SortList, new Comparator<Position>(){ public

Why is my MergeSort so slow in Python?

你说的曾经没有我的故事 提交于 2019-12-01 08:04:01
I'm having some troubles understanding this behaviour. I'm measuring the execution time with the timeit-module and get the following results for 10000 cycles: Merge : 1.22722930395 Bubble: 0.810706578175 Select: 0.469924766812 This is my code for MergeSort: def mergeSort(array): if len(array) <= 1: return array else: left = array[:len(array)/2] right = array[len(array)/2:] return merge(mergeSort(left),mergeSort(right)) def merge(array1,array2): merged_array=[] while len(array1) > 0 or len(array2) > 0: if array2 and not array1: merged_array.append(array2.pop(0)) elif (array1 and not array2) or