Category Archives: Sorting

Sort an array of large numbers

Given an array of numbers where every number is represented as string. The numbers may be very large (may not fit in long long int), the task is to sort these numbers. Examples: Input : arr[] = {“5”, “1237637463746732323”, “12” }; Output : arr[] = {“5”, “12”, “1237637463746732323”}; Input : arr[] = {“50”, “12”, “12”,… Read More »

Sorting Big Integers

Given a array of n positive integers where each integer can have digits upto 106, print the array elements in ascending order. Input: arr[] = {54, 724523015759812365462, 870112101220845, 8723} Output: 54 8723 870112101220845 724523015759812365462 Explanation: All elements of array are sorted in non-descending(i.e., ascending) order of their integer value Input: arr[] = {3643641264874311, 451234654453211101231, 4510122010112121012121}… Read More »

Sort a Matrix in all way increasing order

Given a square matrix of order N*N having distinct elements, the task is to sort given matrix in such a way that its rows, columns and both diagonals (diagonal and anti-diagonal) are in increasing order. Examples: Input : arr[3][3] = {1, 4, 2, 3, 5, 6, 9, 7, 8} Output :{1, 2, 3, 4, 5,… Read More »

An Insertion Sort time complexity question

Question : How much time Insertion sort takes to sort an array of size n in below form? arr[] = 2, 1, 4, 3, 6, 5,….i, i-1, …..n, n-1 Answer : At first look, it seems like Insertion Sort would take O(n2) time, but it actually takes O(n) time How? Let us take a closer… Read More »