create account

Sortieralgorithmen: Merge sort by ozelot47

View this thread on: hive.blogpeakd.comecency.com
· @ozelot47 ·
$6.93
Sortieralgorithmen: Merge sort
Der **Merge sort** ist ein weiteres Sortierverfahren, welches auf dem **divide and conquer** aufbaut. Genauso wie der Quick sort wird die zu sortierende Liste zunächst in mehrere Teillisten aufgeteilt. Anders als beim Quick sort gibt es hier aber kein Pivotelement, wonach eine Liste geteilt wird.
Die Listen werden immer in der Mitte aufgeteilt, sodass beide Teillisten gleich viele Elemente haben. Sollte eine gleichgroße Teilung nicht möglich sein, wenn eine Liste z.B. 5 Elemente hat, so wird entweder die linke oder die rechte Teilliste ein Element mehr haben, je nach Implementierung.

Wenn alle Teillisten sortiert sind, so werden diese miteinander verschmolzen, daher der Name **merge**. Beim Quick sort liegt der Schwerpunkt beim teilen der Listen mithilfe eines Pivotelements, welches immer ermittelt werden muss. Beim Merge sort dagegen werden die Teillisten rekursiv miteinander verschmolzen.

* Merge sort (c++)

```
#include <iostream>

void merge(int list[], unsigned int left, unsigned int split, unsigned int right) {
    unsigned int i, j, k;
    unsigned int subOne = split - left + 1;
    unsigned int subTwo =  right - split;

    /* create temp sub-arrays */
    int L[subOne], R[subTwo];

    /* Copy the elements to the sub-arrays */
    for(i = 0; i < subOne; i++){
        L[i] = list[left + i];
    }

    for(j = 0; j < subTwo; j++){
        R[j] = list[split + 1+ j];
    }

    /* Merge the sub-arrays back into the original array */
    i = 0; // Initial index of first sub-array
    j = 0; // Initial index of second sub-array
    k = left; // Initial index of merged sub-array
    while (i < subOne && j < subTwo) {
        if (L[i] <= R[j]) {
            list[k] = L[i];
            i++;
        } else {
            list[k] = R[j];
            j++;
        }
        k++;
    }

    /* Copy the remaining elements of the left sub-array, if there are any */
    while (i < subOne) {
        list[k] = L[i];
        i++;
        k++;
    }

    /* Copy the remaining elements of the right sub-array, if there are any */
    while (j < subTwo) {
        list[k] = R[j];
        j++;
        k++;
    }
}

void mergeSort(int list[], unsigned int left, unsigned int right) {
    if (left < right) {
        /* splitelement */
        unsigned int split = left + (right - left)/2;

        /* Sort left and right sub-array */
        mergeSort(list, left, split);
        mergeSort(list, split+1, right);

        merge(list, left, split, right);
    }
}

int main(){
    const unsigned int SIZE = 10;
    int list[SIZE] = {5,3,8,1,12,6,7,11,3,2};
    mergeSort(list,0,SIZE-1);
    
    for(unsigned int res = 0; res < SIZE; res++){
        std::cout << list[res] << "\n";
    }
    
    return 0;
}
```

Quelle
https://algs4.cs.princeton.edu/lectures/22Mergesort.pdf [letzter Zugriff: 07.02.2020, 19:00]
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 270 others
👎  
properties (23)
authorozelot47
permlinksortieralgorithmen-merge-sort
categoryde-stem
json_metadata{"tags":["de-stem","deutsch","stem","steemstem","palnet","science","programming"],"links":["https://algs4.cs.princeton.edu/lectures/22Mergesort.pdf"],"app":"steemit/0.1","format":"markdown"}
created2020-02-08 12:11:30
last_update2020-02-08 12:11:30
depth0
children2
last_payout2020-02-15 12:11:30
cashout_time1969-12-31 23:59:59
total_payout_value3.493 HBD
curator_payout_value3.439 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length2,833
author_reputation140,840,715,881,795
root_title"Sortieralgorithmen: Merge sort"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id95,225,836
net_rshares20,873,458,255,037
author_curate_reward""
vote details (335)
@church-of-god ·
# According to the Bible, *In Matthew 17: 1-5, was Matthew in the mountain with Jesus?*
***(Sorry for sending this comment. We are not looking for our self profit, our intentions is to preach the words of God in any means possible.)***
https://youtu.be/NjhaK5Z7Sm8
https://i.postimg.cc/SxmKZFY2/image.jpg
Comment what you understand of our Youtube Video to receive our full votes. We have 30,000 #SteemPower. It's our little way to **Thank you, our beloved friend.**  
Check our [Discord Chat](https://discord.gg/vzHFNd6) 
Join our Official Community: https://beta.steemit.com/trending/hive-182074
👍  
properties (23)
authorchurch-of-god
permlinka0hteow08zf
categoryde-stem
json_metadata""
created2020-02-08 12:19:45
last_update2020-02-08 12:19:45
depth1
children0
last_payout2020-02-15 12:19:45
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length599
author_reputation-10,142,640,475,011
root_title"Sortieralgorithmen: Merge sort"
beneficiaries[]
max_accepted_payout10,000.000 HBD
percent_hbd100
post_id95,226,015
net_rshares1,064,639,272
author_curate_reward""
vote details (1)
@steemstem ·
re-ozelot47-sortieralgorithmen-merge-sort-20200209t153127030z
<div class='text-justify'> <div class='pull-left'> <center> <br /> <img width='200' src='https://res.cloudinary.com/drrz8xekm/image/upload/v1553698283/weenlqbrqvvczjy6dayw.jpg'> </center>  <br/> </div> 

This post has been voted on by the **SteemSTEM curation team** and voting trail. It is elligible for support from @curie and @minnowbooster.<br /> 

If you appreciate the work we are doing, then consider supporting our witness [@stem.witness](https://steemconnect.com/sign/account_witness_vote?approve=1&witness=stem.witness). Additional witness support to the [curie witness](https://steemconnect.com/sign/account_witness_vote?approve=1&witness=curie) would be appreciated as well.<br /> 

For additional information please join us on the [SteemSTEM discord]( https://discord.gg/BPARaqn) and to get to know the rest of the community!<br />

Please consider using the <a href='https://www.steemstem.io'>steemstem.io</a> app and/or including @steemstem in the list of beneficiaries of this post. This could yield a stronger support from SteemSTEM.
👍  
properties (23)
authorsteemstem
permlinkre-ozelot47-sortieralgorithmen-merge-sort-20200209t153127030z
categoryde-stem
json_metadata{"app":"steemstem-bot"}
created2020-02-09 15:31:30
last_update2020-02-09 15:31:30
depth1
children0
last_payout2020-02-16 15:31:30
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,050
author_reputation262,017,435,115,313
root_title"Sortieralgorithmen: Merge sort"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id95,260,701
net_rshares15,566,924,110
author_curate_reward""
vote details (1)