Skip to content

Instantly share code, notes, and snippets.

@andreinechaev
Last active August 29, 2015 14:16
Show Gist options
  • Save andreinechaev/93291b36adcb70e55202 to your computer and use it in GitHub Desktop.
Save andreinechaev/93291b36adcb70e55202 to your computer and use it in GitHub Desktop.
#include <stdio.h>
static const int SIZE = 1000000;
int rank(int key, int arr[]);
int main(int argc, char *argv[]) {
int array[SIZE];
FILE *firstFile = NULL;
if ((firstFile = fopen("largeW.txt", "r")) == NULL) {
return 1;
}
int num;
int count = 0;
while (fscanf(firstFile, "%d", &num) > 0) {
array[count] = num;
count++;
}
FILE *secondFile = NULL;
if ((secondFile = fopen("largeT.txt", "r")) == NULL) {
return -2;
}
int value;
while (fscanf(secondFile, "%d", &value) > 0) {
if (rank(value, array) == -1){
printf("Key %d\n", value);
}
}
fclose(secondFile);
fclose(firstFile);
return 0;
}
int rank(int key, int arr[]) {
int lo = 0;
int hi = SIZE - 1;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
if (key < arr[mid]) hi = mid - 1;
else if (key > arr[mid]) lo = mid + 1;
else return mid;
}
return -1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment