1. 程式人生 > >(排序回顧)歸並排序

(排序回顧)歸並排序

main for using pre turn ray str temp ()

#include <iostream>
using namespace std;
int a[10];
int temp[10];

void mergeArray(int left,int right,int mid){
    int i = left;
    int j = mid + 1;
    int k = 0;

    while( i <= mid && j <= right) {
        if(a[i] < a[j]){
            temp[k++] = a[i++];
        } else {
            temp[k
++] = a[j++]; } } while( i<=mid ){ temp[k++] = a[i++]; } while( j<=right ){ temp[k++] = a[j++]; } k = 0; while(left <= right ){ a[left++] = temp[k++]; } } void mergeSort(int left,int right){ if(left < right){
int mid = (left+right)/2; mergeSort(left,mid); mergeSort(mid+1,right); mergeArray(left,right,mid); } } int main() { int n; cin>>n; for(int i=0;i<n;i++){ cin>>a[i]; } mergeSort(0,n-1); for(int i=0;i<n;i++){ cout<<a[i]<<endl; }
return 0; }

(排序回顧)歸並排序