Merge sort and statistic array reverse order

Source: Internet
Author: User
Tags array sort





1. Merge Sort


Introduction to the algorithm (P19 )


Reference URL:


The realization of the five merging sort of the classical algorithm series of vernacular -morewindows blog- blog channel -csdn.net


http://blog.csdn.net/morewindows/article/details/6678165


#include "stdafx.h" #include <iostream>using std::cout; #define Array_length 11//sort two ordered arrays//Parameters: Data original array, Copy combined into an array, Start,mid,end records the position of two arrays in the original array//mid represents the cutoff position of the first array void Merge (int data[],int copy[],size_t start,size_t mid,si  ze_t end) {//Determine if the input parameters are valid if (data! = nullptr) && (copy! = nullptr) && (Start <= mid) && (Mid <    End) {size_t i = start;  Left array start position size_t j = mid + 1; Right array start position size_t index = start;//Auxiliary array start position while ((I <= mid) && (J <= End) {//Compare two values, the smaller ones into the auxiliary array, and move the pointer forward if (data[ I] <= data[j]) {copy[index++] = data[i++];} else{copy[index++] = data[j++];}} Handles one array traversal, and another array with remaining elements while (I <= mid) {copy[index++] = data[i++];} while (J<=end) {copy[index++] = data[j++];} Collate the sorted data array for (size_t k = 0; k <= end; k++) {data[k] = copy[k];}} Merge algorithm core algorithm void Merge_sort (int data[], int copy[], size_t start, size_t end) {if (data! nullptr) && (copy! = NULL PTR) {if (Start < end) {size_t mid = (start + end)/2;//thought Merge_sort (Data, copy, start, mid); Merge_sort (data, copy, Mid + 1, end);//array sort merge (data, copy, start, Mid, End);}} Else{cout << "Array parameter input error";}} Print array void Printf_array (int data[], size_t length) {for (size_t i = 0; i < length; i++) {cout << data[i] << s Td::endl;}} int _tmain (int argc, _tchar* argv[]) {int data[] = {10,32,9,1,23,42,5,67,88,23,7};//printf_array (data, array_length); int copy[array_length]; Merge_sort (data, copy, 0, array_length-1);p rintf_array (data, array_length); return 0;}


2. using merge sorting to handle related problems, the inverse of the statistics array


"Sword Point offer"P191


InversePais1.cpp: Computes the inverse of an array of n elements to//#include "stdafx.h"//calculates the inverse of the function int get_inversepairs (int data[],int copy[],size_t start,size_t mid,size_t end) {int count = 0;//to determine if the array is empty, and whether the input position parameter is valid if (data!=nullptr) && (copy!=nullptr) &  & (Start <= mid) && (Mid < end)) {int i = mid;int J = End;int index = end;while ((i >= start) && (j >= mid + 1) && (i * J >= 0)) {//Compare whichever is most, put in the secondary array of the lowest if (Data[i] > Data[j]) {copy[index--] = Data[i--];count + = ( J-MID);//reverse to statistics}else{copy[index--] = data[j--];//This time indicates that the latter is greater than the former, does not meet the definition of reverse order, so count does not operate}}//processing the remaining array (note: This situation count will no longer change, As already counted fully) while ((I >= start) && (i >= 0)) {copy[index--] = data[i--];} while ((J >= mid + 1) && (J >= 0)) {copy[index--] = data[j--];} printf ("Raw data:"); for (size_t k = start; K <= End; k++) {printf ("%d", Data[k]);} printf ("\ n Current data:"); for (size_t k = start; K <= End; k++) {Data[k] = Copy[k];p rintf ("%d", Data[k]);} printf ("\ n");p rintf ("Inverse logarithm:%d\n", count);} return count;} int Reverse_pairs (int data[], int copy[], size_t start, size_t end) {if (data = = nullptr) | | (copy = = nullptr)) {return 0;} if (start >= end) {return 0;} Divide-and-conquer strategy int mid = (start + end)/2;int left = reverse_pairs (data, copy, start, mid); int right = Reverse_pairs (data, copy, M ID + 1, end); int count = get_inversepairs (data, copy, start, Mid, end); return left + right + count;} int _tmain (int argc, _tchar* argv[]) {int data[] = {7,5,6,4};const size_t LENGTH = 4;int Copy[length];p rintf ("%d\n", revers E_pairs (data, copy, 0, LENGTH-1)); return 0;}


Merge sort and statistic array reverse order

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.