-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprintSorted1sMIPS.c
More file actions
86 lines (73 loc) · 1.8 KB
/
Copy pathprintSorted1sMIPS.c
File metadata and controls
86 lines (73 loc) · 1.8 KB
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
78
79
80
81
82
83
84
85
86
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#ifndef false
#define false 0
#endif // false
#ifndef true
#define true 1
#endif // true
#define n 120 // size of array
// count number of Ones in a given number
int countOnes (int num) // O(1)
{
int count = 0;
while (num != 0)
{
num &= (num - 1);
count++;
}
return count;
}
// sort both arrays using insertion sort
void doubleInsertionSort(int arr1[], int arr2[], int size) // O(n^2) worst case
{
// insertion sort for sorting the arr2 - higher priority array
for (int i = 1; i < size; i++)
{
// use 2 keys because we need to sort both arrays simultaneously
int key1 = arr2[i];
int key2 = arr1[i];
int j = i - 1;
while (j >= 0 && arr2[j] > key1)
{
arr2[j+1] = arr2[j];
arr1[j+1] = arr1[j];
j--;
}
arr2[j+1] = key1;
arr1[j+1] = key2;
}
// insertion sort for sorting arr1 - lower priority array
for (int i = 0; i < size; i++)
{
int currentCount = arr2[i];
int value = arr1[i];
int j = i - 1;
while (j >= 0 && value < arr1[j] && arr2[j] == currentCount)
{
arr1[j+1] = arr1[j];
j--;
}
arr1[j+1] = value;
}
}
int main()
{
int ints[n];
int counts[n];
// Generate the random numbers, place in ints[]
// store to counts[i] the number of their set bits (synced)
srand(time(0));
int i;
for (i = 0; i < n; i++) {
ints[i] = rand() * 8652 / 4321 ;
counts[i] = countOnes(ints[i]);
}
doubleInsertionSort(ints, counts, n);
// print sorted arrays: [number]:[countOfOnes]
for (i = 0; i < n; i++) {
printf("%d|%d\n", ints[i], counts[i]);
}
return 0;
}