snippetcMinor
Sort and Search Algorithms
Viewed 0 times
andsortsearchalgorithms
Problem
I'm new to programming, but want to build up a good habit of code review so that I can develop best practice. It's been couple of weeks I'm coding in C and my knowledge, so far, is data types, conditions, arrays, loop and, very minimal, pointers.
I'm creating multiple search and sorting algorithms - not only to understand them better, but also solidifying my understanding of concepts.
I'm creating multiple search and sorting algorithms - not only to understand them better, but also solidifying my understanding of concepts.
#include
#include
void swap(int *x, int *);
int midval(int x, int y);
int main(void)
{
// taking count for characters
printf("How many characters are you entering? ");
int count = get_int();
// creating the array
int array[count];
// taking string as an input and converting them to integers
for (int i=0; i array[i+1])
{
swap(&array[i], &array[i+1]);
}
}
j--;
}
while (j!=0);
//binary search
printf("Please enter the character you want to search: ");
int search = atoi(get_string());
int i=0;
do
{
if (search == array[midval(i, count-1)])
{
printf("The character exists in the list.\n");
return 0;
}
else if (search > array[midval(i, count-1)])
{
i = midval(i, count-1) + 1;
}
else if (search < array[midval(i,count-1)])
{
count = midval(i, count-1);
}
}
while (count != i);
printf("The character does not exist in the list.\n");
}
void swap(int *x, int *y)
{
int temp[1];
temp[0] = *x;
*x = *y;
*y = temp[0];
}
int midval(int x, int y)
{
int z = (x+y)/2;
return z;
}Solution
Great work overall implementing these algorithms! I really liked that you extracted some of the common pieces into methods, and even a header file! Also it's great that you're attempting these algorithms on your own, before looking at standard implementations.
For the Bubble Sort portion
For the Binary Search portion
-
A more standard-used naming convention for the variables used in binary search are:
-
You are repeating the call to the
General comments
-
As you did with some parts of your code, you should also extract out separate functions for Bubble Sort and Binary Search. This not only improves reusability, but also makes your code more readable.
-
It will be very helpful as you learn more to look for standard implementations of different algorithms to solidify your understanding and style (after you try to implement them yourself of course!). Generally you want to write code that's readable to other people, which is easier to do when you use standard practices.
For the Bubble Sort portion
- The do-while is generally less readable than using a for-loop, and is preferably only used when it's very necessary - for example, if you need to ensure at least one execution of the loop.
For the Binary Search portion
-
A more standard-used naming convention for the variables used in binary search are:
i:low(indicating the start of the search segment)
count:high(the end of the search segment)
midval():mid(the middle value you compute once, and compare to in a given iteration)
search:target(the value you are searching for)
-
You are repeating the call to the
midval function several times in the code. Instead, you can compute this value once at the start of the loop, and use the computed value later on. General comments
-
As you did with some parts of your code, you should also extract out separate functions for Bubble Sort and Binary Search. This not only improves reusability, but also makes your code more readable.
-
It will be very helpful as you learn more to look for standard implementations of different algorithms to solidify your understanding and style (after you try to implement them yourself of course!). Generally you want to write code that's readable to other people, which is easier to do when you use standard practices.
Context
StackExchange Code Review Q#155197, answer score: 3
Revisions (0)
No revisions yet.