MergeSort | C语言实现归并排序 基本思想如下:将待排序序列R | Interpreter library

 by   chenyufeng1991 C Version: Current License: No License

kandi X-RAY | MergeSort Summary

kandi X-RAY | MergeSort Summary

MergeSort is a C library typically used in Utilities, Interpreter applications. MergeSort has no bugs, it has no vulnerabilities and it has low support. You can download it from GitHub.

C语言实现归并排序 基本思想如下:将待排序序列R[0...n-1]看成是n个长度为1的有序序列,将相邻的有序表成对归并,得到n/2个长度为2的有序表;将这些有序序列再次归并,得到n/4个长度为4的有序序列;如此反复进行下去,最后得到一个长度为n的有序序列。 综上可知,归并排序其实要做两件事情: (1)“分解”——将序列每次折半划分; (2)“合并”——将划分后的序列段两两合并后排序; 那么如何进行合并呢? 在每次合并过程中,都是对两个有序的序列段进行合并,然后排序。这两个有序序列段分别为R[low,mid],R[mid+1,high]。先将它们合并到一个局部的暂存数组R2中,合并完成后再将R2复制到R中。R[low,mid]称为第一段,R[mid+1,high]称为第二段,每次从两个段中取出一个记录进行关键字的比较,将较小者放入R2中。最后将各段中余下的部分直接复制到R2中。经过这样,R2已经是一个有序的序列,再将其复制回R中,一次合并排序就完成了。.
Support
    Quality
      Security
        License
          Reuse

            kandi-support Support

              MergeSort has a low active ecosystem.
              It has 6 star(s) with 1 fork(s). There are 2 watchers for this library.
              OutlinedDot
              It had no major release in the last 6 months.
              MergeSort has no issues reported. There are no pull requests.
              It has a neutral sentiment in the developer community.
              The latest version of MergeSort is current.

            kandi-Quality Quality

              MergeSort has no bugs reported.

            kandi-Security Security

              MergeSort has no vulnerabilities reported, and its dependent libraries have no vulnerabilities reported.

            kandi-License License

              MergeSort does not have a standard license declared.
              Check the repository for any license declaration and review the terms closely.
              OutlinedDot
              Without a license, all rights are reserved, and you cannot use the library in your applications.

            kandi-Reuse Reuse

              MergeSort releases are not available. You will need to build from source code and install.

            Top functions reviewed by kandi - BETA

            kandi's functional review helps you automatically verify the functionalities of the libraries and avoid rework.
            Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of MergeSort
            Get all kandi verified functions for this library.

            MergeSort Key Features

            No Key Features are available at this moment for MergeSort.

            MergeSort Examples and Code Snippets

            No Code Snippets are available at this moment for MergeSort.

            Community Discussions

            QUESTION

            Implement barrier with pthreads on C
            Asked 2021-Jun-15 at 18:32

            I'm trying to parallelize a merge-sort algorithm. What I'm doing is dividing the input array for each thread, then merging the threads results. The way I'm trying to merge the results is something like this:

            ...

            ANSWER

            Answered 2021-Jun-15 at 01:58

            I'm trying to parallelize a merge-sort algorithm. What I'm doing is dividing the input array for each thread, then merging the threads results.

            Ok, but yours is an unnecessarily difficult approach. At each step of the merge process, you want half of your threads to wait for the other half to finish, and the most natural way for one thread to wait for another to finish is to use pthread_join(). If you wanted all of your threads to continue with more work after synchronizing then that would be different, but in this case, those that are not responsible for any more merges have nothing at all left to do.

            This is what I've tried:

            Source https://stackoverflow.com/questions/67977544

            QUESTION

            Sort an array of pairs using mergesort gets stackoverflow
            Asked 2021-Jun-13 at 07:38

            I have the array A = arrayOf(Pair(1,1),Pair(0,9),Pair(3,8),Pair(4,0),Pair(6,6),Pair(10,7),Pair(5,1),Pair(7,3)) in Kotlin and I want to sort it with mergesort based on the X component of the pairs.

            So far I have this

            ...

            ANSWER

            Answered 2021-Jun-13 at 07:38

            Your recursive procedure is missing the base case, without this your procedures falls in a loop where it calls itself indefinitely and ultimately causes the stack overflow.

            When it comes to writing recursive procedures one must understand that there are two cases that must be clearly defined.

            1. Recursive case : When recursive procedure calls itself (Recursion)

            2. Base case : When recursion bottoms out (In your case this happens when array contains single item, in which case its already sorted, no need to recurse)

            Base case is very important property of a recursive procedure, since its responsible for stopping the procedure from falling in an endless recursion.

            Source https://stackoverflow.com/questions/67954157

            QUESTION

            inversion count mergesort in C
            Asked 2021-Jun-09 at 08:53

            A permutation of integers from 1 to n is a sequence a1, a2, ..., an, such that each integer from 1 to n is appeared in the sequence exactly once.

            Two integers in а permutation form an inversion, when the bigger one is before the smaller one.

            As an example, in the permutation 4 2 7 1 5 6 3, there are 10 inversions in total. They are the following pairs: 4–2, 4–1, 4–3, 2–1, 7–1, 7–5, 7–6, 7–3, 5–3, 6–3.

            Input n and array[n] 2<=n<=100,000

            First I solved problem with bubble sorting but then i met time complexity problem.

            Second I solved it mergesort but I didn't do well

            Here is my cord

            ...

            ANSWER

            Answered 2021-Apr-02 at 15:27

            I cannot find some implementation bits in your code that divides the arrays into sub-arrays based on the index(as quick sort sorts based on value) kindly have a look at the code provided below

            Source https://stackoverflow.com/questions/66921074

            QUESTION

            "Quick Sort works well on small arrays of data, but merge sort is better for large arrays"
            Asked 2021-Jun-08 at 01:05

            When comparing quick sort to other sorts I have heard something along the lines of "quick sort works well for small amounts of data".

            And an example of this comment is here:

            Merge sort can work well on any type of data sets irrespective of its size (either large or small). whereas Quick sort cannot work well with large datasets.

            https://www.geeksforgeeks.org/quick-sort-vs-merge-sort

            Specifically, I am looking at mergesort vs quicksort and have heard several times "quicksort is better for small amounts of data, and mergesort is better for large amounts of data"

            I understand quick sort has its advantages over merge sort as well for larger amounts of data (locality of reference, no extra space needed, etc.).

            However, I am struggling to understand why quicksort would be especially good/better than mergesort for smaller amounts of data.

            My work:

            From a quick program on my part (which is far from optimized running the basic versions of each sort) this seemed to hold true:

            For array of size 10:

            ...

            ANSWER

            Answered 2021-Jun-08 at 00:57

            quicksort with little or no code to avoid worst case behavior is generally faster than merge sort for random numbers (although in this case, radix sort would be faster still). On a processor with 16 registers, such as a PC in 64 bit mode, a 4 way merge sort (using nested if else instead of a heap) is about the same speed or faster than quicksort for any data.

            You need to consider more than the comparisons. In general, merge sort does more moves but fewer compares than quicksort. An example where this helps merge sort to be faster is sorting an array of pointers to strings, where the time to move pointers is much faster than comparing strings.

            Both quicksort and merge sort run times can be improved by using insertion sort for small sub-arrays, from about 32 to 128 elements.

            Source https://stackoverflow.com/questions/67880156

            QUESTION

            Merge sort algorithm in ANSI C
            Asked 2021-Jun-07 at 01:28

            I'm learning ANSI C and for a class I have to implement a merge sort algorithm in it. I'm following the book's guide. But, for some reason I can't have it working.

            Only two digits from my list got in the right position. And for the others, new digits are created. I have no idea why this happens, I think it is the way I create the temp arrays L and R (I'm using 1.0 / 0.0 to set the last element to +INFINITY). But I'm not sure if that is the reason.

            Here is my code so far:

            ...

            ANSWER

            Answered 2021-Jun-07 at 01:10

            There is my merge sort algorithm, and the function returns the number of inversions.

            Source https://stackoverflow.com/questions/67864853

            QUESTION

            MergeSort in python
            Asked 2021-Jun-02 at 07:25
            def get_random_array(n):
                return [random.randint(0, 10000) for _ in range(n)]
            
            def test_sorting_algorithm(algorithm):
                for _ in range(100):
                    A = get_random_array(random.randint(0, 1000))
                    A_sorted = algorithm(A)
                    assert A_sorted == sorted(A), "FAIL!"
            
            
            def merge(A,l,m,r):
            
                i = l
                j = m+1
                new = []
            
                while i <= m and j <= r:
                    if A[i] <= A[j]:
                        new.append(A[i])
                        i += 1
                    else:
                        new.append(A[j])
                        j += 1
            
                while i <= m:
                    new.append(A[i])
                    i += 1
               
                while j <= r:
                    new.append(A[j])
                    j += 1
            
                return new
            
            
            def mergeSort_rec(A, l, r): 
            
            
                if l < r:       
                    m = (l+(r-1))//2  # Same as (l+r)//2, but avoids overflow for large l and h 
                    # Sort first and second halves 
                    mergeSort_rec(A, l, m) 
                    mergeSort_rec(A, m+1, r)                                 
                    merge(A, l, m, r) 
            
            def mergeSort(B):
                A = B[:] # Copy the array just because we decided to return a sorted copy of the original array 
                mergeSort_rec(A, 0, len(A)-1)
                return A
            
            A = get_random_array(10)
            
            test_sorting_algorithm(mergeSort)
            
            ...

            ANSWER

            Answered 2021-Jun-02 at 07:25

            This statement doesn't make sense:

            Source https://stackoverflow.com/questions/67797134

            QUESTION

            Trying to do the mergeSort, and the index out of bounds
            Asked 2021-May-30 at 19:43

            I'm trying to do the mergeSort. and when I run the test. It said my index is out of bounds. Is there any mistake I missed?

            Error is: java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3

            ...

            ANSWER

            Answered 2021-Apr-13 at 02:21

            Let's debug the mergeSort method with a of length 3.

            L is initialized with size of 1, and R with size of 2

            You will enter the loop.

            First iteration: y is 0, y is less than a.length

            Second iteration: y is 1, y is less than a.length. Oops! L in index 1 is OutOfBounds.

            Source https://stackoverflow.com/questions/67067804

            QUESTION

            I have tried to implement mergesort and am unable to find the exact problem of my code. The program shows no errors but I do not get a desired output
            Asked 2021-May-28 at 06:59

            Tried implementing mergesort, below is code. Let me know if anything is wrong. Is there any any error in my merge function? unable to point to the exact problem in my code. Any help regarding the same would be highly appreciated.

            ...

            ANSWER

            Answered 2021-May-28 at 06:59

            There are 2 bugs is the code:

            • computing the mid point as int mid = start + (end - 1) / 2; is incorrect. You should instead write:

            Source https://stackoverflow.com/questions/67731445

            QUESTION

            Sorting an array of type Object
            Asked 2021-May-27 at 21:17

            I need to write a method which sorts (merge sort) an array of Objects and has this exact signature:

            void mergeSort(Object[] a)

            Generics are not allowed at all.

            The method will be passed a covariant array, ex: String[] or Integer[] . My problem now is that I don't know how to access a Comparator of these Objects or sort them otherwise without one. The algorithm for sorting the elements itself is not the problem.

            ...

            ANSWER

            Answered 2021-May-27 at 21:00

            I'm assuming that the task is designed to show you how awful things were before generics, because this is exactly how the Java <1.5 signature for sorting an array would look like :)

            Since the task says "natural ordering", it's implied that the passed Object implements the Comparable interface which has a method compareTo(). So to access it you should simply cast the Object to Comparable. This is exactly what they say in Java 1.4.2 documentation: https://www2.cs.duke.edu/csed/java/jdk1.4.2/docs/api/java/util/Arrays.html#sort(java.lang.Object[])

            The key idea behind generics is that when you try to pass, say, a Cactus object into a sort() method, it would fail in compile time, rather than throw a ClassCastException in runtime when JVM would try to cast the Cactus class to Comparable interface.

            Source https://stackoverflow.com/questions/67728460

            QUESTION

            Recursion not exiting on the base case in merge sort
            Asked 2021-May-24 at 17:53

            I am learning python and was implementing 2D matrix merge sort and my code goes to infinite loop while calling merge sort and i am unable to find the reason for the same.

            ...

            ANSWER

            Answered 2021-May-24 at 17:53

            Your midpoints aren't actually midpoints. Consider l == 5 and r == 8. Then l + r//2 == 5 + 4 == 9.

            Instead, you want

            Source https://stackoverflow.com/questions/67676613

            Community Discussions, Code Snippets contain sources that include Stack Exchange Network

            Vulnerabilities

            No vulnerabilities reported

            Install MergeSort

            You can download it from GitHub.

            Support

            For any new features, suggestions and bugs create an issue on GitHub. If you have any questions check and ask questions on community page Stack Overflow .
            Find more information at:

            Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items

            Find more libraries
            CLONE
          • HTTPS

            https://github.com/chenyufeng1991/MergeSort.git

          • CLI

            gh repo clone chenyufeng1991/MergeSort

          • sshUrl

            git@github.com:chenyufeng1991/MergeSort.git

          • Stay Updated

            Subscribe to our newsletter for trending solutions and developer bootcamps

            Agree to Sign up and Terms & Conditions

            Share this Page

            share link

            Explore Related Topics

            Consider Popular Interpreter Libraries

            v8

            by v8

            micropython

            by micropython

            RustPython

            by RustPython

            otto

            by robertkrimen

            sh

            by mvdan

            Try Top Libraries by chenyufeng1991

            BaiduMap-TrafficAssistant

            by chenyufeng1991Java

            Android-HowOld

            by chenyufeng1991Java

            BinaryTree

            by chenyufeng1991C

            LinkedList_HeadNode

            by chenyufeng1991C

            Queue_Array

            by chenyufeng1991C