Showing posts from August, 2017

C++: Selection sort using STL

Selection sort is an sorting algorithm. It has O(n2) time complexity. It has performance advantages over more complicated algorithms in certain situations, particularly where auxiliary memory is limited.

It divides the input list or the array in two parts:
1. Sorted array
2. Unsorted array

The minimum element from the unsorted array is picked and placed at the end of sorted array in each iteration.

C++: Program to find smallest element is an array which is repeated exactly 'k' times

This is a problem from GeeksForGeeks. I have tried to write different code for the problem.

The problem says we have an array of elements . We have to find the smallest element which is repeated exactly k times.

C++: Implementation of Merge Sort

Merge sort is a sorting algorithm which uses Divide-and-Conquer approach.

A merge sort works as follows: 
1. Divide the unsorted list into n sublists, each containing 1 element (a list of 1 element is considered sorted). 

2. Repeatedly merge sublists to produce new sorted sublists until there is only 1 sublist remaining. This will be the sorted list.  Source Wikipedia

C++: Swapping Node Links in Linked List

Here we are going to swap the links of the node instead of data in the node. 

Here we are using swapLinks() function to swap the links of the node. We are using search() function which returns the node of the value entered and insert() to insert the values.  

You can refer C++: Singly Linked List for the code of search() and insert().
For eg.

LinkedList1 = {10, 15, 12, 13, 28, 14, 16}swapLinks(10, 13)LisnkedList1 = {13, 15, 12, 10, 28, 14, 16}

C++: Insertion Sort using STL (Sorting)

An insertion sort is generally described simply as: read each element in turn, and insert it in the right position among the previous read (and sorted) elements.

It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort.

However, insertion sort provides several advantages:

➧ Simple implementation

C++: Sorting Elements according to Frequency

We are going to write a program to sort the elements of an array by Frequency. If two elements have the same frequency of occurrence, then they are sorted in increasing order.

For eg. an array has { 2, 3, 2, 4, 5, 12, 12, 3, 4, 3 }

Then the output will be

3 3 3 2 2 4 4 12 12 5

Top 10 Web Sites Every Programmer Should Visit Daily

Computer programming is one of the fastest growing field. If you are a programmer then you can be hired by a top tech company or you can also work as a freelancer and earn decent amount. So the question arises, how to learn to code ? Initially it was very difficult to learn programming. As there was no internet and you have to relay on books or your colleagues’ code. Now you can find tons of material on the internet. You can learn any computer programming language you want. I am going to tell you some sites which you should visit regularly. 

C++: STL Iterators

Iterators arepointer like object which are used to point at the memory addressess of STL containers.

Properties of Iterator 

We can dereference an iterator in order to access the value towhich itrefers. That is, if p is an iterator, *pis defined.

We can assign one iterator to another. That is, if p and q areiterators, theexpression p = qis defined.

We can compare one iterator to another for equality. That is, if p and q are iterators, the expressions p == q and p != qare defined.

We can move an iterator through all the elements of a container. This canbe satisfied by defining ++p and p++ for an iterator p .

C++: Queue implementation using Linked List (Data Structure)

We have already learnt about Queue int the post C++: Implementation of Queue using Array (Data Structure). Now we are implementing Queue using Linked List which we have learnt in C++: Singly Linked List using Template (Data Structure).

Here is the C++ program for implementing Queue using Linked List.

Popular Posts

C++: Breadth First Search program using Adjacency Matrix

Check if the String is Palindrome (C++ & Java Program)

C++: Depth First Search program using Adjacency Matrix (Graph Algorithm)