Sort in C++ standard template library

Two elements of complex data types are not directly comparable. Consider the student structure defined as below struct Student { int rollNo; char name[25]; char grade; }; Student arr[MAX_NUM_STUDENTS]; Objects of Student type are not directly comparable. To sort array arr, we need to specify how one student compares with another student. If there are […]

Nearest number with non-decreasing digits

Given a number N given in the form of array, with each index storing one digit of the number. Write code to change the number to the greatest number less than N, such that digits of that number are in non-decreasing order. For example: INPUT ARRAY: {1, 4, 3} (representing number 143) OUTPUT : 139 […]

Selection Sort

Selection sort is an in-place, comparison sorting algorithm. Like bubble sort it also divides the given array in two sub arrays sorted and unsorted, divided by an imaginary wall. Initially all the elements will be in the unsorted list. Each pass will pick the smallest element from the unsorted list and add it at the end […]

Optimized bubble sort algorithm

What is Bubble Sort. Write algorithm of mention the Time & Space complexity of the Algorithm. Also suggest improvements which will improve the best case running time of Algorithm to O(n). Solution: Bubble Sort is a sorting algorithm which compares two adjacent elements and swap them if they are not in the right order. To […]