#include <iostream>using namespacestd;Const intLENGTH = -;intTemp[length];intCount =0;voidMerge (intArray[],intLowintMidintHigh );voidMergeSort (intArray[],intLowintHigh );/** * Merge two arrays [Low, mid], (mid, High] to array [Low, high] * @param array to sort arrays * @param low start subscript * @param mid dividing line * @param hi GH Stop subscript*/voidMerge (intArray[],intLowintMidintHigh ) { intTempindex =Low ; intLeftindex =Low ; intRightindex = mid +1; while(Leftindex <= mid && Rightindex <=High ) { if(Array[leftindex] <Array[rightindex]) {Temp[tempindex+ +] = array[leftindex++]; } Else //Array[rightindex] is smaller than the elements of [Leftindex, mid], which are generated by reverse order{Temp[tempindex+ +] = array[rightindex++]; Count+ = (Mid-leftindex +1); } } while(Leftindex <=mid) {Temp[tempindex+ +] = array[leftindex++]; } while(Rightindex <=High ) {Temp[tempindex+ +] = array[rightindex++]; } for(inti = low; I <= high; ++i) {Array[i]=Temp[i]; }}voidMergeSort (intArray[],intLowintHigh ) { if(Low = = high)return; intMid = (low + high)/2; MergeSort (Array, low, mid); MergeSort (Array, Mid+1, high); Merge (Array, Low, mid, High);}intMain () {intArray[] = {0,4,2,6, A,Ten,8,7,5}; MergeSort (Array,0,8); for(Auto Val:array) {cout<< Val <<" "; } cout<<Endl; cout<<"--------------------------"<<Endl; cout<< Count <<Endl; return 0;}
View Code
Description: Requires compiler support C++11
PS c:\users\administrator\desktop\codes> g++-O. \inversenumber.exe. \inversenumber.CPP-std=c++ OnePS C:\Users\Administrator\Desktop\codes>. \inversenumber.exe0 2 4 5 6 7 8 Ten A-------------------------- APS C:\Users\Administrator\Desktop\codes> g++-vusing built-inchspecs. COLLECT_GCC=f:\greensoftware\codeblocks\tdm-gcc\bin\g++. Execollect_lto_wrapper=f:/greensoftware/codeblocks/tdm-gcc/bin/. /libexec/GCC/mingw32/5.1.0/lto-wrapper.exeTarget:mingw32Configured with:../.. /.. /src/GCC-5.1.0/configure--build=mingw32--enable-languages=ada,c,c++,fortran,lto,objc,obj-c++--enable-libgomp--enable-lto-- Enable-graphite--enable-libstdcxx-debug--enable-threads=posix--enable-version-specific-runtime-libs--enable-fully-dynamic-string--enable-libstdcxx-threads--enable-libstdcxx- Time--with-gnu-LD--disable-Werror--disable-nls--disable-win32-registry--disable-symvers--enable-cxx-flags='-fno-function-sections-fno-data-secTions-dwinpthread_static'--prefix=/mingw32tdm--with-local-prefix=/mingw32tdm--with-pkgversion=tdm-1--enable-sjlj-exCeptions--with-bugurl=http://tdm-gcc.tdragon.net/bugsThread Model:posixGCCVersion5.1.0(tdm-1) PS C:\Users\Administrator\Desktop\codes>
Merge sort to find array inverse logarithm