-
Notifications
You must be signed in to change notification settings - Fork 0
/
auxArrayFunctions.cpp
95 lines (86 loc) · 2.53 KB
/
auxArrayFunctions.cpp
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
87
88
89
90
91
92
93
94
95
#include <iostream>
#include <ctime>
#include <cmath>
#include <cstdlib>
using namespace std;
#include "auxArrayFunctions.h"
void displayArray(int *arr, int len){
for (int i = 0; i < len; i++)
cout << arr[i] << "\t";
cout << endl;
}
void createAlreadySortedArrays(int *&arr1, int *&arr2, int *&arr3, int N){
if (N <= 0){
arr1 = arr2 = arr3 = NULL;
return;
}
arr1 = new int [N];
arr2 = new int [N];
arr3 = new int [N];
for (int i = 0; i < N; i ++)
arr1[i] = arr2[i] = arr3[i] = i;
}
void createRandomArrays(int *&arr1, int *&arr2, int *&arr3, int N){
if (N <= 0){
arr1 = arr2 = arr3 = NULL;
return;
}
arr1 = new int [N];
arr2 = new int [N];
arr3 = new int [N];
srand( time(NULL) );
for (int i = 0; i < N; i ++){
arr1[i] = rand() % (2 * N);
arr2[i] = arr3[i] = arr1[i];
}
}
void attemptOneSwap(int *arr, int N, int K, int currIndex){
int minIndex, maxIndex, swapIndex;
arr[currIndex] - K >= 0 ? minIndex = arr[currIndex] - K : minIndex = 0;
arr[currIndex] + K < N ? maxIndex = arr[currIndex] + K : maxIndex = N - 1;
swapIndex = rand() % (maxIndex - minIndex + 1) + minIndex;
if (arr[swapIndex] - K <= currIndex && currIndex <= arr[swapIndex] + K){
int temp = arr[swapIndex];
arr[swapIndex] = arr[currIndex];
arr[currIndex] = temp;
}
}
double averageDistance(int *arr, int N, int K){
double avgDist = 0.0;
for (int i = 0; i < N; i++){
int diff = abs(arr[i] - i);
if (diff > K)
cout << "Error (please inform the instructor)" << endl;
avgDist += diff;
}
if (N == 0)
return 0;
return avgDist / N;
}
void createNearlySortedArrays(int *&arr1, int *&arr2, int *&arr3, int N, int K){
if (N <= 0){
arr1 = arr2 = arr3 = NULL;
return;
}
int minIndex, maxIndex;
arr1 = new int [N];
arr2 = new int [N];
arr3 = new int [N];
srand( time(NULL) );
for (int i = 0; i < N; i ++)
arr1[i] = i;
int iterNo = 0;
while (iterNo < 10) {
for (int i = 0; i < N; i ++)
attemptOneSwap(arr1, N, K, i);
for (int i = N - 1; i >= 0; i--)
attemptOneSwap(arr1, N, K, i);
double d = averageDistance(arr1, N, K);
cout << d << endl;
if (d > K / 2)
break;
iterNo++;
}
for (int i = 0; i < N; i ++)
arr2[i] = arr3[i] = arr1[i];
}