Pick up on 3.4 Switch
This commit is contained in:
@@ -0,0 +1,136 @@
|
|||||||
|
/*
|
||||||
|
*
|
||||||
|
* Exercise 3-1. Our binary search makes two tests inside the loop, when one would
|
||||||
|
* suffice (at the price of more tests outside). Write a version with only one test
|
||||||
|
* inside the loop and measure the difference in run-time.
|
||||||
|
*
|
||||||
|
* Author: Kun Deng
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <limits.h>
|
||||||
|
#include <float.h>
|
||||||
|
|
||||||
|
#define ARRAY_SIZE 99900000
|
||||||
|
|
||||||
|
long v[ARRAY_SIZE];
|
||||||
|
|
||||||
|
long binsearch(long x, long v[], long n);
|
||||||
|
long binsearch_improved(long x, long v[], long n);
|
||||||
|
|
||||||
|
void populate_array(const long size);
|
||||||
|
void print_array(const long v[], long size);
|
||||||
|
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
extern long v[];
|
||||||
|
|
||||||
|
populate_array(ARRAY_SIZE);
|
||||||
|
// print_array(v, ARRAY_SIZE);
|
||||||
|
|
||||||
|
long x = 901103;
|
||||||
|
|
||||||
|
printf("Starting binary search. Trying to find %ld...\n", x);
|
||||||
|
|
||||||
|
long index = binsearch(x, v, ARRAY_SIZE);
|
||||||
|
printf("Regular binary search complete\n");
|
||||||
|
long index_improved = binsearch_improved(x, v, ARRAY_SIZE);
|
||||||
|
printf("Improved binary search complete\n");
|
||||||
|
|
||||||
|
if (index < 0)
|
||||||
|
{
|
||||||
|
printf("%ld was not found in the array\n", x);
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("Binary search: Value %ld is at index %ld of the array\n", x, index);
|
||||||
|
printf("Binary search improved: Value %ld is at index %ld of the array\n", x, index_improved);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
long binsearch(long x, long v[], long n)
|
||||||
|
{
|
||||||
|
long low, mid, high;
|
||||||
|
|
||||||
|
low = 0;
|
||||||
|
high = n - 1;
|
||||||
|
|
||||||
|
while (low <= high)
|
||||||
|
{
|
||||||
|
mid = (low + high) / 2;
|
||||||
|
|
||||||
|
if (x < v[mid])
|
||||||
|
{
|
||||||
|
high = mid - 1;
|
||||||
|
}
|
||||||
|
else if (x > v[mid])
|
||||||
|
{
|
||||||
|
low = mid + 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return mid;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
long binsearch_improved(long x, long v[], long n)
|
||||||
|
{
|
||||||
|
long low, mid, high;
|
||||||
|
|
||||||
|
low = 0;
|
||||||
|
high = n - 1;
|
||||||
|
mid = (low + high) / 2;
|
||||||
|
|
||||||
|
for (; (low <= high) && x != v[mid]; mid = (low + high) / 2)
|
||||||
|
{
|
||||||
|
if (x < v[mid])
|
||||||
|
{
|
||||||
|
high = mid - 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
low = mid + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (v[mid] == x)
|
||||||
|
{
|
||||||
|
return mid;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void populate_array(long size)
|
||||||
|
{
|
||||||
|
extern long v[];
|
||||||
|
long i = 0;
|
||||||
|
v[i] = 1; // First element will start at 1
|
||||||
|
|
||||||
|
for (i = 1; i != size; ++i)
|
||||||
|
{
|
||||||
|
long elem = v[i];
|
||||||
|
v[i] = elem + (i * 2) + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_array(const long v[], long size)
|
||||||
|
{
|
||||||
|
printf("array is: ");
|
||||||
|
|
||||||
|
for (long i = 0; i != size; ++i)
|
||||||
|
{
|
||||||
|
printf("%ld ", v[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("\nWith %ld elements\n", size);
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
/*
|
||||||
|
*
|
||||||
|
* Demonstration of the if-else statement
|
||||||
|
*
|
||||||
|
* Author: Kun Deng
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
const int x = 15;
|
||||||
|
const int compared_value = 1024;
|
||||||
|
|
||||||
|
// The expression within the if statment evaluates the expression.
|
||||||
|
// The expression is true if the expression is a non-zero number.
|
||||||
|
// Otherwise it's false if 0. The reason for this if you're famililar
|
||||||
|
// with programming languages that have bool or boolean is the in C,
|
||||||
|
// there isn't a boolean type. Or well at least in not standards before
|
||||||
|
// C99.
|
||||||
|
if ((x < compared_value) != 0)
|
||||||
|
{
|
||||||
|
printf("Statement is true\n");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("Statement is false\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
/*
|
||||||
|
*
|
||||||
|
* Exercise 3-
|
||||||
|
*
|
||||||
|
* Author: Kun Deng
|
||||||
|
*/
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
/*
|
||||||
|
*
|
||||||
|
* Brief demonstration of statements and blocks
|
||||||
|
*
|
||||||
|
* Author: Kun Deng
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
// Below are some statements. A statement is considered one when
|
||||||
|
// the semicolon is the last character of the statement. The
|
||||||
|
// semicolon serves as a statement terminator.
|
||||||
|
|
||||||
|
int x = 48;
|
||||||
|
x /= 4;
|
||||||
|
printf("Print statment %d\n", x);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user