Print array of strings in sorted order without copying one string into another
Given an array of n strings. The task is to print the strings in sorted order. The approach should be such that no string should be copied to another string during the sorting process.
Examples:
Input : {"geeks", "for", "geeks", "quiz")
Output : for geeks geeks quiz
Input : {"ball", "pen", "apple", "kite"}
Output : apple ball kite pen
Approach: It has the following steps:
- Maintain another array indexed_arr which stores/maintain the index of each string.
- We can apply any sorting technique to this indexed_arr.
An Illustration:
--> str[] = {"world", "hello"}
--> corresponding index array will be
indexed_arr = {0, 1}
--> Now, how the strings are compared and
accordingly values in indexed_arr are changed.
--> Comparison process:
if (str[index[0]].compare(str[index[1]] > 0
temp = index[0]
index[0] = index[1]
index[1] = temp
// after sorting values of
// indexed_arr = {1, 0}
--> for i=0 to 1
print str[index[i]]
This is how the strings are compared and their
corresponding indexes in the indexed_arr
are being manipulated/swapped so that after the sorting process
is completed, the order of indexes in the indexed_arr
gives us the sorted order of the strings.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
void printInSortedOrder(string arr[], int n)
{
int index[n];
int i, j, min;
for (i=0; i<n; i++)
index[i] = i;
for (i=0; i<n-1; i++)
{
min = i;
for (j=i+1; j<n; j++)
{
if (arr[index[min]].compare(arr[index[j]]) > 0)
min = j;
}
if (min != i)
{
int temp = index[min];
index[min] = index[i];
index[i] = temp;
}
}
for (i=0; i<n; i++)
cout << arr[index[i]] << " " ;
}
int main()
{
string arr[] = { "geeks" , "quiz" , "geeks" , "for" };
int n = 4;
printInSortedOrder(arr, n);
return 0;
}
|
Java
class GFG {
static void printInSortedOrder(String arr[], int n) {
int index[] = new int [n];
int i, j, min;
for (i = 0 ; i < n; i++) {
index[i] = i;
}
for (i = 0 ; i < n - 1 ; i++) {
min = i;
for (j = i + 1 ; j < n; j++) {
if (arr[index[min]].compareTo(arr[index[j]]) > 0 ) {
min = j;
}
}
if (min != i) {
int temp = index[min];
index[min] = index[i];
index[i] = temp;
}
}
for (i = 0 ; i < n; i++) {
System.out.print(arr[index[i]] + " " );
}
}
static public void main(String[] args) {
String arr[] = { "geeks" , "quiz" , "geeks" , "for" };
int n = 4 ;
printInSortedOrder(arr, n);
}
}
|
Python 3
def printInSortedOrder(arr, n):
index = [ 0 ] * n
for i in range (n):
index[i] = i
for i in range (n - 1 ):
min = i
for j in range (i + 1 , n):
if (arr[index[ min ]] > arr[index[j]]):
min = j
if ( min ! = i):
index[ min ], index[i] = index[i], index[ min ]
for i in range (n):
print (arr[index[i]], end = " " )
if __name__ = = "__main__" :
arr = [ "geeks" , "quiz" , "geeks" , "for" ]
n = 4
printInSortedOrder(arr, n)
|
C#
using System;
public class GFG {
static void printInSortedOrder(String []arr, int n) {
int []index = new int [n];
int i, j, min;
for (i = 0; i < n; i++) {
index[i] = i;
}
for (i = 0; i < n - 1; i++) {
min = i;
for (j = i + 1; j < n; j++) {
if (arr[index[min]].CompareTo(arr[index[j]]) > 0) {
min = j;
}
}
if (min != i) {
int temp = index[min];
index[min] = index[i];
index[i] = temp;
}
}
for (i = 0; i < n; i++) {
Console.Write(arr[index[i]] + " " );
}
}
static public void Main() {
String []arr = { "geeks" , "quiz" , "geeks" , "for" };
int n = 4;
printInSortedOrder(arr, n);
}
}
|
Javascript
<script>
function printInSortedOrder(arr,n)
{
let index = new Array(n);
let i, j, min;
for (i = 0; i < n; i++) {
index[i] = i;
}
for (i = 0; i < n - 1; i++) {
min = i;
for (j = i + 1; j < n; j++) {
if (arr[index[min]]>(arr[index[j]]) ) {
min = j;
}
}
if (min != i) {
let temp = index[min];
index[min] = index[i];
index[i] = temp;
}
}
for (i = 0; i < n; i++) {
document.write(arr[index[i]] + " " );
}
}
let arr=[ "geeks" , "quiz" , "geeks" , "for" ];
let n = 4;
printInSortedOrder(arr, n);
</script>
|
Output
for geeks geeks quiz
Time Complexity: O(n2).
Auxiliary Space: O(n).
The approach can have its usage when we have to minimize the number of disc writes as in the case of an array of structures. The structure values are compared, but their values are not being swapped, instead, their index is maintained in another array, which is manipulated to keep the indexes in an order which represents the sorted array of structures.
Last Updated :
12 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...