Guest User

Merge

a guest
May 22nd, 2012
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.08 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void Spoji(float A[],int i,int k,int j) {
  5.      int I=i, J=k+1, K=0;
  6.      float *B = new float [j-i+1];
  7.      while (I<=k && J<=j)
  8.            if (A[I]<=A[J])
  9.               B[K++]=A[I++];
  10.            else
  11.                 B[K++]=A[J++];
  12.      if (I>k)
  13.         while (J<=j)
  14.               B[K++] = A[J++];
  15.      else
  16.          while (I<=k)
  17.                B[K++] = A[I++];
  18.      for (int I=0;I<=j-i;I++)
  19.          A[i+I]=B[I];
  20.      delete []B;
  21. }
  22. void MSort(float A[],int i, int j) {
  23.      if (i<j) {
  24.         int k=(i+j)/2;
  25.         MSort(A,i,k);
  26.         MSort(A,k+1,j);
  27.         Spoji(A,i,k,j);
  28.      }
  29. }
  30.  
  31. void MSort(float A[],int N) {
  32.      MSort(A,0,N-1);
  33. }
  34. int main () {
  35.     int N, I;
  36.     do {
  37.        cout << "N = ";
  38.        cin >> N;
  39.     } while (N < 2 && N > 1000);
  40.     float *A = new float [N];
  41.     for (int i = 0; i < N; i++) {
  42.         cout << "A[" << i << "] = ";
  43.         cin >> A[i];
  44.     }
  45.     MSort(A,N);
  46.     for (int i = 0; i < N; i++)
  47.         cout << A[i] << " ";
  48.     cout << endl;
  49.     delete []A;
  50.     return 0;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment