Skip to main content

Posts

Showing posts with the label C

Hash Tables : Scratching the Surface

Have you ever worked with key-value data stores where you can attach one value to a key and store it in some form to perform easy lookups ? If yes, then i guess you have already seen hash tables in action. Hash Tables are so common these days that you don't even realise when you start using them but not a lot of people are familiar with the internals of hash tables and how these can be used in real world problems like cryptography. This a series of articles all about hashing. So sit tight and enjoy the journey. What Problems Does Hashing Solve ? Simple Implementation of Hash Table Collision Resolution Chaining Open Addressing Desining Hash Functions How Python Dictionaries Work Hashing in Action What Problems Does Hashing Solve ? Hash Tables are the most popular data structure in computer science. We forget ...

The Art of Debugging

Writing unorganised code for something to work is an easy task, just put anything anywhere and see if the result is coming or not. If the result comes out in first attempt then you are really lucky but normal people like me are not. Then what we should do to check where the problem is occuring, we put some print statements (or console.log) to check the result in various places and observe where the output is deviating from its path. Well, it itself is a good practise of debugging the code but it does not work always especially not with large projects. So what should we do then ? According to me, we should give debugger a chance to find the bugs in our program. Debugging is an important part of writing programs because it teaches you to write unit tests for the parts of the program where it can fail. To perform debugging, we need a debugger and thankfully we have one, gdb (GNU debugger). In this article we will start by seeing some basic debugging techniques which involves logging the...

See More of C

Why more of c ? Function Pointers Search and Sort Multi-Threaded Programming Time Functions Random Numbers String Conversion Process Creation and System commands execution DataBase Handling CGI Scripting More Links Why digging more of C ? Being a high level language and the features C support there is not a need to ask why should we learn more about C. C programs are relatively faster than most of other high level languages like python or java. That may be because C programs are compiled first or may be because C libraries and fucntions are more hardware oriented. But whatever is the reason, C programs are way better. I can show you an example of it to you. There is a program which corrects the spelling of a mis-spelled word like it changes "somethinh" to "something". Now here is the comparison between the programs written in C and than that written in python Property Program i...

C-Mate : Your companion for C in Sublime Text

What is C-Mate ? When it comes to writing code then either we choose an IDE like codeblocks or ecllipse, OR we use some text editor like notepad++ or sublime text. Sublime text is a very powerful, flexible and free text editor. You can install any package you want in sublime which help you writing codes easily. C-Mate is also such a sublime package which will help you write C code efficiently on ubuntu. It consists of many useful snippets, pre-coded functions and a powerful build system. Why C-Mate Although there is a pre-installed c++ sublime package which helps you writing c as well as c++ codes but the feature it lags is the code running in the terminal. People like writing code in sublime but when it comes to build and run the code then they need to go the directory containing the code file and then build and run it from there. It gets so annoying when we debug the code. No sublime package allows to run the code within the sublime. Here C-Mate makes the exception. Its build system...

String Cloning

String Copy Functions Coders, programmers and all the rookies who are new to programming always use string copy functions.As the name suggests, these are standard library functions which copies one string into other.Here we'll discuss two standard functions: strcpy() : which copies the whole string into another strncpy() : which copies a part of one string into another Syntax The first function takes 2 arguments: strcpy(char *target, char *source) ; The second function takes 3 arguments: strncpy(char *target, char *source, int n) ; Here "n" represents the number of characters which are to be copied into another string Working Example #include #include int main() { int n ; char source[25], target[25] ; strcpy(source, "fetch-info.blogspot.com") ; strncpy(target, source, 10) ; printf("Source String: %s\n", source) ; printf("Target String: %s",target) ; return 0 ; } OUTPUT Source String: fetch-info.bl...

Bubble Sort

Bubble Sorting Among variaous other sorting algorithm, bubble sort algorithm is one of the popular and frequently used algorithm to sort elements either in ascending or descending order. Bubble sort algorithm starts by comparing the first two elements of an array and swapping if necessary, i.e., if you want to sort the elements of array in ascending order and if the first element is greater than second then, you need to swap the elements but, if the first element is smaller than second, you mustn't swap the element. Then, again second and third elements are compared and swapped if it is necessary and this process go on until last and second last element is compared and swapped. This completes the first step of bubble sort. If there are  n  elements to be sorted then, the process mentioned above should be repeated  n-1  times to get required result. But, for better performance, in second step, last and second last elements are not compared becuase, the prop...

Insertion Sorting-Some Extra Stuff

Insertion Sorting Intro:- Insertion sort is one of the many sorting algorithms which implements sorting in a very elegant way. It is much less efficient on large lists than more advanced algorithms such as  quicksort , heapsort, or  merge sort .Its algo is easy to understand.If the first few objects are already sorted, an unsorted object can be inserted in the sorted set in proper place. This is called insertion sort. An algorithm consider the elements one at a time, inserting each in its suitable place among those already considered (keeping them sorted). Insertion sort is an example of an incremental  algorithm; it builds the sorted sequence one number at a time. This is perhaps the simplest example of the incremental insertion technique, where we build up a complicated structure on n items by first building it on n − 1 items and then making the necessary changes to fix things in adding the last item. The given sequences are typically stored in arrays....

Selection Sort

Selection Sorting Selection sort algorithm starts by compairing first two elements of an array and swapping if necessary, i.e., if you want to sort the elements of array in ascending order and if the first element is greater than second then, you need to swap the elements but, if the first element is smaller than second, leave the elements as it is. Then, again first element and third element are compared and swapped if necessary. This process goes on until first and last element of an array is compared. This completes the first step of selection sort. If there are  n  elements to be sorted then, the process mentioned above should be repeated  n-1  times to get required result. But, for better performance, in second step, comparison starts from second element because after first step, the required number is automatically placed at the first (i.e, In case of sorting in ascending order, smallest element will be at first and in case of sorting in descending...

Algorithmic Complexity

Algorithmic Complexity Introduction Algorithmic complexity is concerned about how fast or slow particular algorithm performs. We define complexity as a numerical function T(n) - time versus the input size n . We want to define time taken by an algorithm without depending on the implementation details. But you agree that T(n) does depend on the implementation! A given algorithm will take different amounts of time on the same inputs depending on such factors as: processor speed; instruction set, disk speed, brand of compiler and etc. The way around is to estimate efficiency of each algorithm asymptotically . We will measure time T(n) as the number of elementary "steps" (defined in any way), provided each such step takes constant time. Let us consider two classical examples: addition of two integers. We will add two integers digit by digit (or bit by bit), and this will define a "step" in our computational model. Therefore, we say that addition of two n-bit...

Sorting-A New Approach

Run-Time Sorting There are so many ways to sort numbers like quick sort , heap sort, selection sort, merge sort , insertion sort etc but there is another way to sort numbers.This method sorts the numbers during run-time.It implies that as you enter the new number, the number is stored in the sorted order in the array.The best things about this method are.. It does not require a link-list. There is no limit on the numbers of input.You can enter as many numbers as you want. Algorithm Firstly create a memory block using malloc(), then input a number. Find the place where the number is to be stored in the array. Its very simple and can easily be implemented.Its complexity analysis hasn't done yet.The following pic can help you understand the method.

Merge-Sort

Merge Sorting Merge sorting also works on the principle of "divide and conquer".The algorithm of merge sort is quite different from that of quick-sort but its best case performance is also O(nlog(n)).The step by step process is- Divide the unsorted list into n sublists, each containing 1 element (a list of 1 element is considered sorted). Repeatedly merge sublists to produce new sorted sublists until there is only 1 sublist remaining. This will be the sorted list. How it is actually implemented is described here.... Firstly divide the given array of n elements into n individual numbers. Then compare the adjacent elements and merge them in the sorted order. Then compare the corresponding elements of the resulting lists and arrange them in the sorted order. And finally the sorted list will be obtained. The following link will help you in understanding it. Want to see the example code..?? CLICK HERE Complexity Analysis of Merge Sort Worst Case Ti...

Quick-Sort

Quick Sort, as the name suggests, sorts any list very quickly. Quick sort is not stable search, but it is very fast and requires very less aditional space. It is based on the rule of Divide and Conquer (also called partition-exchange sort ). This algorithm divides the list into three main parts : Elements less than the Pivot element Pivot element Elements greater than the pivot element As the image suggests, a pivot is selected at first which is 4 in our case.It then compares the other members with the pivot and divides the list into three main parts as discussed above. The next step is to call this function recursively.It will return one number if only one member is left in any of the three parts. Complexity Analysis of Quick Sort Worst Case Time Complexity : O(n 2 ) Best Case Time Complexity : O(n log n) Average Time Complexity : O(n log n) Space Complexity : O(n log n) Space required by quick sort is very less, only O(n log n) additional space is re...

Creating An Array of Variable Size-2

Another Method for Array of Variable Size So, I have already introduced you to a new method to create an array of variable size but there is a drawback in that method. (Those who have not red that method yet can have a look at it. Click here ) And the drawback is that you have to create another function to create that array and sometimes you need to enter a variable of which the array size is selected and that variable is to be entered in the middle of your program then you have to call the another function to create the array in middle of your program.And suppose if the variable in inside a loop then array is created so many times but you need only the first case.In that scenario, the previous method would not be suitable. But for small programs that method is still valuable. Here i am presenting you another method to do our job. That method is based on the technique of dynamic memory allocation.Here what we do is that we create a pointer variable and then required...

Declare an array of variable length

Declare an array of variable length in C language A lot of people keep asking about an array of variable length.Then they find a more often answer in the books, on the net i.e. use dynamic memory allocation.Though it is very important concept because it uses the memory in run time which reduces the execution time of your program but so many people are not used to this method which makes them fuzzy.I know another method to declare an array of variable length which is very comfortable to use. There you go.... Firstly create a function which will recieve the input from the user which is the length of your array.In most cases it is done in the main function.So how it looks... main() { int n ; scanf("%d", &n) ; func(n) ; } Then create a function to which you'll send the length variable as an arguement.In our case it in func() Now the final step, declare the array with the previous variable as its length. func(int k) { int arr[k] ; .....your code..... } I ...