#include<stdio.h>
      int first(int arr[], int low, int high, int x, int n)
      {
          if (high >= low) {
              int mid = low + (high - low) / 2;
              if ((mid == 0 || x > arr[mid - 1]) && arr[mid] == x)
                  return mid;
              if (x > arr[mid])
                  return first(arr, (mid + 1), high, x, n);
              return first(arr, low, (mid - 1), x, n);
          }
          return -1;
      }
      void sortAccording(int A1[], int A2[], int m, int n)
      {
          int temp[m], visited[m];
          for (int i = 0; i < m; i++) {
              temp[i] = A1[i];
              visited[i] = 0;
          }
          for(int i=0; i<m; i++){
              for(int j=i+1; j<m; j++){ if(temp[i]>temp[j]){
                      int x= temp[i];
                      temp[i]= temp[j];
                      temp[j]=x;
                  }
              }
          } 
          int ind = 0;
          for (int i = 0; i < n; i++) {
              int f = first(temp, 0, m - 1, A2[i], m);
              if (f == -1)
                  continue;
              for (int j = f; (j < m && temp[j] == A2[i]); j++) {
                  A1[ind++] = temp[j];
                  visited[j] = 1;
              }
          }
          for (int i = 0; i < m; i++)
              if (visited[i] == 0)
                  A1[ind++] = temp[i];
      }
      void printArray(int arr[], int n)
      {
          for (int i = 0; i < n; i++)
              printf("%d ", arr[i]);
          printf("\n");
      }
      // Driver Code
      int main()
      {
          int arr1[] = { 20, 1, 20, 5, 7, 1, 9, 39, 6, 18, 18 };
          int arr2[] = { 20, 1, 18, 39 };
          int m = sizeof(arr1) / sizeof(arr1[0]);
          int n = sizeof(arr2) / sizeof(arr2[0]);
          sortAccording(arr1, arr2, m, n);
          printArray(arr1, m);
          return 0;
      }
      
      C language logo

      Sort an array according to the order defined

      0

      0

      avatar
      drzyadav

      0 Comments

        Add Comment

        Log in to add a comment

        Codiga - All rights reserved 2022.