mergesort

Merge Sort in place for python (Cant find what is wrong)

一个人想着一个人 提交于 2019-12-04 18:24:28
I was reading about merge sort(In place) in my algorithm book (Intro to algorithms 3rd edition Cormen), and I decided to implement it in Python. The problem is that I can't find what I am doing wrong... I saw some code in C++, but even with that I can't fix it. Here is my code: def MERGE(A,start,mid,end): L=[0]*(mid - start + 1) for i in range(len(L) - 1): L[i] = A[start+i] L[len(L) - 1] = 100000 # centinel, (fix) R=[0]*(end - mid + 2) for j in range(len(R) - 1): R[j] = A[mid+j] R[len(R) - 1] = 100000 i = 0 j = 0 k = start for l in range(k,end): if(L[i] < R[j]): A[l] = L[i] i = i + 1 else: A[k

How far does recursion execute into your function in C++?

让人想犯罪 __ 提交于 2019-12-04 17:04:17
I've written recursive functions with the guidance of a friend who is teaching me C++ (as a first language). However, I don't really understand what is going on. He helped me (and the SO community, as well) write a merge sort function. std::vector<int> mergeSort(std::vector<int> original) //code here to create two vectors, farray and sarray, out of the //original std::vector<int> original ; each is half the length, //note: if the size % 2 != 0, sarray takes the odd int farray = mergeSort(farray); sarray = mergeSort(sarray); //code here merges the two into a single, final sorted std::vector In

Merge Sort in R

非 Y 不嫁゛ 提交于 2019-12-04 16:56:28
I am self studying the book "Introduction to Algorithms" by Cormen et alli. In their book, they use pseudo-code which assumes that arrays are passed by pointer (by reference). This is different from R (where objects are passed by value), so I am having some difficulties trying to translate their pseudo-code as close as possible, especially when recursion is involved. Most of the time, I have to implement things a lot differently. For example, with the Merge Sort algorithm, they define the Merge Function (which I think I have translated correctly) and the recursive MergeSort function (where

Merge sort implementation

拈花ヽ惹草 提交于 2019-12-04 14:54:11
I am new to c++ and was trying develop a code for merge sort. I tested it with a sample array of size 5 but the answer put out by the code is not right. I can't figure what's going wrong. Here is my code: #include <iostream> #include <cstring> #include <sstream> #include <fstream> #include <iomanip> using namespace std; void merge(int, int, int, int*); void merge_sort(int low, int high, int* p){ int pivot; static int i(1); if (high>low) { cout << "calling merge_sort: "<<i<<endl; i++; pivot = low + ((high - low)/2); cout << pivot << endl; merge_sort(low, pivot, p); merge_sort(pivot+1, high, p);

Mergesort - Is Bottom-Up faster than Top-Down?

99封情书 提交于 2019-12-04 10:37:00
问题 I've been reading "Algorithms, 4th Ed" by Sedgewick & Wayne, and along the way I've been implementing the algorithms discussed in JavaScript. I recently took the mergesort examples provided in the book to compare top-down and bottom-up approaches... but I'm finding that bottom-up is running faster (I think). See my analysis on my blog. - http://www.akawebdesign.com/2012/04/13/javascript-mergesort-top-down-vs-bottom-up/ I have not been able to find any discussion that says one method of

Why is insertion sort always beating merge sort in this implementation?

房东的猫 提交于 2019-12-04 10:16:13
I don't understand: why is my insertion sort implementation beating merge sort every time, for any size of n ? public List<Int32> InsertionSort(List<Int32> elements, Boolean ascending = true) { for (Int32 j = 1; j < elements.Count; j++) { Int32 key = elements[j]; Int32 i = j - 1; while (i >= 0 && (elements[i].CompareTo(key) > 0) == ascending) elements[i + 1] = elements[i--]; elements[i + 1] = key; } return elements; } public List<Int32> MergeSort(List<Int32> elements, Boolean ascending = true) { Sort(elements, 0, elements.Count - 1); return elements; } private void MergeSort(List<Int32>

Example how to use predsort(:Compare, +List, -Sorted) in prolog

梦想的初衷 提交于 2019-12-04 09:18:27
I want to order a custom list. The list I want to order will be in this form... [n(_,2,_),n(_,1,_),n(_,3,_)] I have wrote a comparator cheaper(n(_,C1,_),n(_,C2,_)) :- C1>C2. How do I use this with predsort. I wrote a sorting algorithm using bubble sort, but I have very large lists so it very slow. Is it possible to do predsort(cheaper, [n(_,2,_),n(_,1,_),n(_,3,_)] , X). Thank you :) Try this : cheaper(>, n(_,C1,_),n(_,C2,_)) :- C1>C2. cheaper(<, n(_,C1,_),n(_,C2,_)) :- C1<C2. cheaper(=, n(_,C1,_),n(_,C2,_)) :- C1=C2. Be aware that predsort works like sort, there are no doubles ! If you want to

Mergesort Getting an Error in F#

梦想与她 提交于 2019-12-04 06:48:52
问题 let rec merge = function | ([], ys) -> ys | (xs, []) -> xs | (x::xs, y::ys) -> if x < y then x :: merge (xs, y::ys) else y :: merge (x::xs, ys) let rec split = function | [] -> ([], []) | [a] -> ([a], []) | a::b::cs -> let (M,N) = split cs (a::M, b::N) let rec mergesort = function | [] -> [] | L -> let (M, N) = split L merge (mergesort M, mergesort N) mergesort [5;3;2;1] // Will throw an error. I took this code from here StackOverflow Question but when I run the mergesort with a list I get an

C# merge sort performance

拥有回忆 提交于 2019-12-04 03:06:22
just a quick note, this is not homework. I'm just trying to brush up on my algorithms. I'm playing around with MergeSort in C# and I've written a recursive method that can sort based on Generics: class SortAlgorithms { public T[] MergeSort<T> (T[] unsortedArray) where T : System.IComparable<T> { T[] left, right; int middle = unsortedArray.Length / 2; left = new T[middle]; right = new T[unsortedArray.Length - middle]; if (unsortedArray.Length <= 1) return unsortedArray; for (int i = 0; i < middle; i++) { left[i] = unsortedArray[i]; } for (int i = middle; i < unsortedArray.Length; i++) { right[i

complexity of mergesort with linked list

痴心易碎 提交于 2019-12-04 01:52:42
问题 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