Merge sort modify----to be able to row negative numbers

Source: Internet
Author: User

The first few days to write the merge sorting algorithm does not want to enter a specific number to be sorted, after the array input is complete, with-1 end. In this way the program can not row 1, some are not very cool, forget, just add an input array with the length of it. The code is as follows:

#include <iostream>using namespace Std;int a[100];int flag;int n;void vector_initial (); void Print_vector (); void Merge_sort (int p,int r); void Merge_combine (int p,int q,int r); int main () {vector_initial (); Merge_sort (0,n);p rint_vector (); return 0;} void Vector_initial () {int i=0;cout<< "Please enter the number of numbers to sort:"; Cin>>n;while (i<n) {cin>>a[i];i++;} n=n-1;} void Print_vector () {for (int i=0;i<n;i++) cout<<a[i]<< ""; Cout<<a[n]<<endl;} void Merge_sort (int p,int R)//merge sort {int q;if (p<r) {q= (p+r)/2; Merge_sort (P,Q); Merge_sort (Q+1,R); Merge_combine (P,q,r);}} void Merge_combine (int p,int q,int r) {int L[100];int r[100];int n1=q-p+1;int n2=r-q;for (int i=1;i<=n1;i++) L[i]=a[p+ i-1];for (int j=1;j<=n2;j++) R[J]=A[Q+J]; l[n1+1]=10000; r[n2+1]=10000;i=1;j=1;for (int k=p;k<=r;k++) {if (L[i]<=r[j]) {a[k]=l[i];i++;} else{a[k]=r[j];j++;}}}

  

Merge sort modify----to be able to row negative numbers

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.