antiDigest / Algorithms

So that someone keeps trying to find the solutions !

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

A Small Suggestion

duaraghav8 opened this issue · comments

Hey!
If I may, I have a little suggestion.
It is considered best practice, when writing code, to implement the algorithm in a generic form, i.e., your program should run regardless of the size of the array (in this case). For instance, your for loop (in Insertion Sort) uses the condition j < 6 (because the length of the array you used is 6). This program will break if I use any array which doesn't have the length 6.

I know that you have understood the algorithm by heart and just want to demonstrate this fact, but get in the habit of writing generic code from day 1. Believe me, its going to do wonders for you in future!

(for example, take the Insertion Sort Algo, implement it inside a separate function (lets call it insertion_sort ()) which accepts an array as an argument (or a pointer to the array plus its length). This way, I can use any sort of array (with any primitive data type like char, float, int, etc.) of any size, simply call a function from main () and poof! Its done!).
This means all I have to write in my file is:

include <stdio.h>

//COPY YOUR FUNCTION DEFINITION
int main () {
int A [] = {-2, 893, 89, 29, 189};
length = sizeof (A) / sizeof (A [0]);
insertion_sort (A, length);
//logic to print the sorted array

return (0);
}

Try to make it as smooth as possible for someone else who might want to use your code.

Thanks a lot !
I'll take care of it the next time I am implementing a code !