-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelativeSort.c
77 lines (63 loc) · 1.46 KB
/
relativeSort.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
Karan Chawla
Algorithm: Sort one array based on the other given array
*/
#include <stdio.h>
#include <stdlib.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;
}
int comp (const void * elem1, const void * elem2)
{
int f = *((int*)elem1);
int s = *((int*)elem2);
if (f > s) return 1;
if (f < s) return -1;
return 0;
}
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;
}
qsort (temp, sizeof(temp)/sizeof(*temp), sizeof(*temp), comp);
int k=0; //for index of sorted output of a1
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[k++] = temp[j];
visited[j]=1;
}
}
for(int i=0;i<m;i++){
if(visited[i]==0){
a1[k++]= temp[i];
}
}
}
void printArray(int *a, int size){
for(int i=0;i<size;i++){
printf("%d ", a[i]);
}
}
int main(void){
int A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8};
int A2[] = {2, 1, 8, 3};
int m = sizeof(A1)/sizeof(A1[0]);
int n = sizeof(A2)/sizeof(A2[0]);
sortAccording(A1, A2, m, n);
printArray(A1,m);
return 0;
}