Quick sort is a divide-and-conquer sorting algorithm. To understand quick-sort, let us look at a high-level description of the algorithm. Divide: If the sequence S has 2 or more elements, select an element x from S to be your pivot. Any arbitrary element, like the last, will do. Remove all the elements of S and divide them into 3 sequences: L, which holds S’s elements less than x E, which holds S’s elements equal to x G, which holds S’s elements greater than x Recurse: Recursively sort L and G Read More
Postfix
The sum of X and Y is written as X+Y where + is the operator while X and Y are the operands. We have always learnt to write the sum of two numbers as X + Y; this notation is know as infix. Here, we’ll be talking about the postfix notation of representing arithmetic operations. XY+ // postfix The relative position of the operator with respect to the operands tells whether the expression is written in postfix or infix notation. As the above expression shows, when the position of the Read More
Traversing and Searching a Linear Linked List
Traversing a list A linear list can be traversed in two ways In order traversal Reverse order traversal In order Traversal To traverse the linear linked list, we walk the list using the pointers, and process each element until we reach the last element. .cf { font-family: Lucida Console; font-size: 9pt; color: black; background: white; } .cl { margin: 0px; } .cb1 { color: green; } .cb2 { color: blue; } .cb3 { color: maroon; } void traverseinorder(node *head) { while(head!=NULL) { printf(“%dn”,head->info); head=head->next; } } Reverse Order Read More
Throw and Catch Mechanism
Throw Mechanism When an exception, which is desired to be handled, is detected, it is thrown using the throw statement. It can be used in any one of the following form: throw(exception) ; throw exception ; throw ; // used for re-throwing an exception The object passed to the throw statement may be of any type, including constants. It is also possible to throw objects not intended for error handling. When an exception is thrown, it will be caught by the catch statement associated with the try block, i.e., the Read More
Stacks
Stacks are one of the commonly used data structures. A stack is also known as a last in first out (LIFO) system. It can be considered as a linear list in which insertion and deletion can take place only at one end called the top. This structure operates in much the same way as a stack of trays. This article covers the representation of stacks using arrays. For the representation using self-referential structures, please refer to Stacks using Dynamic Memory Allocation. Here is a stack of trays: The following figures Read More
Stacks using Dynamic Memory Allocation
A stack that dynamically allocates memory for each of its elements (just like a linked list) is also known as a linked stack. The array-based representation of a stack suffers from the following limitations. The size of the stack must be known in advance. We may come across a situation when we need to push more elements in the stack, but cannot do so because we’ve reached the maximum size. However, a stack is an abstract data structure and should not become full. Hence, abstractly, it is always possible to Read More
Overloading Unary Operators
Unary operators are the ones that operate on one operand, one such operator is the unary minus (-) operator which is used to change the sign of the operand it acts upon. This operator works well with basic data types such as int, float, etc.. In this section we will see how to overload this operator so that it can work the same way for user defined data types as it does for basic data types, i.e. change the sign of the operand. The unary minus operator function does not Read More
Inline Function
One of the main objectives of using functions in a program is to save some memory space, which becomes appreciable when a function is likely to be called many times. However, every time a function is called, it takes a lot of extra time in executing a series of instructions for task such as jumping to the function, saving register, pushing arguments into the stack, and returning to the calling function. When a function is small, a substantial percentage of execution time may be spent in such overheads. One solution Read More
Override
Sometimes a subclass inherits a method from a superclass that doesn’t quite fit its needs. Perhaps the subclass inherited twenty methods and just one of them wasn’t quite right. In that case, the subclass would override that method by redefining that method itself. This override does not affect the method as defined in the superclass.
Heaps
A heap is a binary tree that satisfies the following properties: Shape property Order property By the shape property, we mean that a heap must be a complete binary tree. A complete binary tree has all its levels filled, except possibly for the last, where the leaves must be located as far to the left as possible. By the order property, we mean that for every node in the heap, the value stored in that node is greater than or equal to the value in each of its children. A Read More
Share on: