Dynamic Memory Allocation Explained With Simple Example

Dynamic memory allocation is the process of allocating memory at run time by the program based on the need of the program. Dynamic Memory allocation is done from the heap memory available in the system and handling of memory (creation and deletion) is totally handled by the programmer. In case, programmer forgets to cleanup the allocated dynamic memory block, it will lead into memory leak and this memory block will be blocked for further use until program restarts.

Methods available to allocate dynamic memory at run time are

  • malloc () – allocates one memory block of requested size.
  • calloc () – allocates multiple blocks of memory and initialize it with 0.
  • realloc () – increases or decreases the size of previously allocated memory block.
  • free () – frees the memory allocated by above functions.
(more…)
Dynamic Memory Allocation Explained With Simple Example Read More

Heap sort explained with simple example

Heap sort is one of the fastest sorting algorithm, which works on Divide and Conquer algorithm. In this sorting algorithm, all the data elements are inserted into a heap (max or min) and then removes item from the root of the heap will give the sorted(descending or ascending) data list.

Pros:
1) Faster algorithm and best performance O(nlogn).
2) Simple implementation.
3) No extra space is needed.
4) Good for cases to find biggest/smallest or top k biggest/smallest element finding kind of scenarios.

Cons:
1) Not a stable sort means position of relative identical data items is not guaranteed.

(more…)

Heap sort explained with simple example Read More

Quick sort explained with simple example

Quick sort is one of the fastest sorting algorithm, which works on Divide and Conquer algorithm. In this sorting algorithm, an element is picked as a pivot element and list is divided into sub-lists around the pivot element. These divided sub-lists finally sorted into which results into sorted initial list.

Pros:
1) Faster algorithm and best performance O(nlogn) is for worst distribution case scenario.
2) Simple implementation due to recursive nature.
3) No extra space is needed.

Cons:
1) Worst case scenario performance is O(n*n).
2) Not good for Linked list kind of sorting data where memory is not contiguous.

(more…)

Quick sort explained with simple example Read More

Merge sort explained with simple example

Merge sort is one of the simplest sorting algorithm, which works on Divide and Conquer algorithm. In this sorting algorithm, input data is divided into two halves/sublist which then acts as input data and furthur divided into two halves so that finally every sublist contains a single element. Finally, these sublists are merged in sorted order which results into sorted data.

Pros:
1) Simple algorithm and hence efficient for small list of data.
2) Good for sorting data represented in Linked list.
3) This algorithm is stable (performance is equal in all cases) and insensitive to the distribution of initial data.

Cons:
1) Extra space needed in O(n).

(more…)

Merge sort explained with simple example Read More

Selection sort explained with simple example

Selection sort is one of the simplest sorting algorithm, which traverse through the list, picks the minimum item from the unsorted list and place it in its place in the final sorted list.
This sorting algorithm contains (n – 1) traversal.

Pros:
1) Simple algorithm and hence efficient for small list of data.
2) No extra memory needed.
3) Good for cases when list is almost sorted (only few misplaced items).
4) Very less number of swap operation(O(n)), as in every pass only 1 swap of data is needed at max.

Cons:
1) Very slow algorithm. Not good for very large set of data.

(more…)

Selection sort explained with simple example Read More

Insertion sort explained with simple example

Insertion sort is one of simplest sorting algorithm which traverse through the list, picking one item at a time and place it in its place in the final sorted list. This sorting algorithm contains (n – 1) traversal.

Pros:
1) Simple algorithm and hence efficient for small list of data.
2) No extra memory needed.
3) Good for cases when list is almost sorted (only few misplaced items).

Cons:
1) Very slow algorithm. Not good for very large set of data.

(more…)

Insertion sort explained with simple example Read More

Bubble sort explained with simple example

Bubble sort is one of the simplest sorting algorithm which traverse through the list, compares items at adjacent places, and swaps them to make list sorted. After every pass of list one or more item is placed at its correct position. If in a pass, no swaps are performed then it means list becomes sorted.

Pros:
1) Simple algorithm.
2) No extra memory needed.
3) Good for cases when list is almost sorted (only few misplaced items).

Cons:
1) Very slow algorithm. Not good for very large set of data.

(more…)

Bubble sort explained with simple example Read More

Sorting algorithms explained in simple manner

Sorting is an operation in computer science via which data is being arranged in an ordered sequence. There are lots of efficient algorithms are already present to perform sorting operation which we are going to discuss here.

Some of the most used sorting algorithms are as follows:

1) Bubble Sort
2) Insertion Sort
3) Merge Sort
4) Selection Sort
5) Quick Sort
6) Heap Sort

Comparison of various Sorting algorithms in terms of time complexity for best and worst case:

(more…)

Sorting algorithms explained in simple manner Read More