# Sort a String in decreasing order of values associated after removal of values smaller than X

Given an integer X and a string str consisting of space-separated texts and numbers placed alternately, the task is to sort the string such that texts and numbers appear in decreasing order of numbers associated after removal of all numbers less than X. If two strings have the same values then sort them in the lexicographic order of the string.

Examples:

Input: X = 79, str = “Geek 78 for 99 Geeks 88 Gfg 86”
Output:
for 99 Geeks 88 Gfg 86
Explanation: “Even 99” is removed, since the number associated to it is smaller than X(= 79) Now, the reordered string string based on decreasing order of numbers associated is “Bob 99 Suzy 88 Alice 86”.

Input: X = 77, str = “Axc 78 Czt 60”
Output: Axc 78

Approach: The idea is to use the Bubble Sort technique. Follow the steps below to solve the problem:

• Split the string in to a list, then remove the entries that are less than the given value i.e. X.
• Sort the list based on the number associated with it using bubble sort method.
• If the numbers are not equal, sort the numbers in decreasing order and simultaneously sort the names.
• If the numbers are equal then sort them lexicographically.
• Swap both the strings and the number together in order to keep them together.

Below is the implementation of the above approach:

## C++

 `// C++ Code``#include ``#include ``#include ``#include ``using` `namespace` `std;` `// Function to split the input string into a list``vector tokenizer(string str) {``  ``vector list;``  ``string temp = ``""``;``  ``for` `(``int` `i = 0; i < str.length(); i++) {``    ``if` `(str[i] == ``' '``) {``      ``list.push_back(temp);``      ``temp = ``""``;``    ``}``    ``else` `{``      ``temp += str[i];``    ``}``  ``}``  ``list.push_back(temp);``  ``return` `list;``}` `// Function to sort the given list based on values at odd indices of the list``string sortListByOddIndices(vector list, ``int` `x) {``  ``int` `l = list.size();` `  ``// Function to remove the values less than the given value x``  ``for` `(``int` `i = l - 1; i > 0; i -= 2) {``    ``if` `(stoi(list[i]) < x) {``      ``list.erase(list.begin() + i - 1, list.begin() + i + 1);``    ``}``  ``}` `  ``l = list.size();` `  ``for` `(``int` `i = 1; i < l; i += 2) {``    ``for` `(``int` `j = 1; j < l - i; j += 2) {``      ``// Compares values at odd indices of the list to sort the list``      ``if` `(list[j] < list[j + 2]``          ``|| (list[j - 1] > list[j + 1] && list[j] == list[j + 2])) {``        ``swap(list[j], list[j + 2]);``        ``swap(list[j - 1], list[j + 1]);``      ``}``    ``}``  ``}` `  ``string sortedList;``  ``for` `(``int` `i = 0; i < list.size(); i++) {``    ``sortedList += list[i];``    ``sortedList += ``" "``;``  ``}` `  ``return` `sortedList;``}` `// Driver code``int` `main()``{``  ` `  ``string str = ``"Axc 78 Czy 60"``;``  ``int` `x = 77;` `  ``// Function call``  ``vector list = tokenizer(str);` `  ``string sortedList = sortListByOddIndices(list, x);` `  ``cout << sortedList;``  ``return` `0;``}` `// This code is contributed by akashish__`

## Python3

 `# Python Program to implement``# the above approach` `# Function to split the input``# string into a list``def` `tokenizer(``Str``):``    ``List` `=` `Str``.split()``    ``return` `List` `# Function to sort the given list based``# on values at odd indices of the list``def` `SortListByOddIndices(``List``, x):` `    ``l ``=` `len``(``List``)` `# Function to remove the values``# less than the given value x``    ``for` `i ``in` `range``(l ``-` `1``, ``0``, ``-``2``):``        ``if` `int``(``List``[i]) < x:``            ``del``(``List``[i ``-` `1``: i ``+` `1``])` `    ``l ``=` `len``(``List``)` `    ``for` `i ``in` `range``(``1``, l, ``2``):``        ``for` `j ``in` `range``(``1``, l ``-` `i, ``2``):` `            ``# Compares values at odd indices of``            ``# the list to sort the list``            ``if` `List``[j] < ``List``[j ``+` `2``] \``            ``or` `(``List``[j ``-` `1``] > ``List``[j ``+` `1``] \``            ``and` `List``[j] ``=``=` `List``[j ``+` `2``]):``                ` `                ``List``[j], ``List``[j ``+` `2``] \``                ``=` `List``[j ``+` `2``], ``List``[j]``                ` `                ``List``[j ``-` `1``], ``List``[j ``+` `1``] \``                ``=` `List``[j ``+` `1``], ``List``[j ``-` `1``]` `    ``return` `' '``.join(``List``)`  `# Driver Code``Str` `=` `"Axc 78 Czy 60"``x ``=` `77` `# Function call``List` `=` `tokenizer(``Str``)` `Str` `=` `SortListByOddIndices(``List``, x)` `print``(``Str``)`

## Javascript

 `// Function to split the input string into a list``function` `tokenizer(str) {``  ``const list = str.split(``' '``);``  ``return` `list;``}` `// Function to sort the given list based on values at odd indices of the list``function` `sortListByOddIndices(list, x) {``  ``var` `l = list.length;` `  ``// Function to remove the values less than the given value x``  ``for` `(let i = l - 1; i > 0; i -= 2) {``    ``if` `(Number(list[i]) < x) {``      ``list.splice(i - 1, 2);``    ``}``  ``}` `  ``l = list.length;` `  ``for` `(let i = 1; i < l; i += 2) {``    ``for` `(let j = 1; j < l - i; j += 2) {``      ``// Compares values at odd indices of the list to sort the list``      ``if` `(list[j] < list[j + 2]``        ``|| (list[j - 1] > list[j + 1] && list[j] === list[j + 2])) {``        ``[list[j], list[j + 2]] = [list[j + 2], list[j]];``        ``[list[j - 1], list[j + 1]] = [list[j + 1], list[j - 1]];``      ``}``    ``}``  ``}` `  ``return` `list.join(``' '``);``}` `// Driver code``const str = ``'Axc 78 Czy 60'``;``const x = 77;` `// Function call``const list = tokenizer(str);` `const sortedList = sortListByOddIndices(list, x);` `console.log(sortedList);`

## C#

 `// C# Code``using` `System;``using` `System.Collections.Generic;``using` `System.Linq;` `public` `class` `GFG{``    ``// Function to split the input string into a list``    ``static` `List<``string``> Tokenizer(``string` `str)``    ``{``        ``List<``string``> list = ``new` `List<``string``>();``        ``string` `temp = ``""``;``        ``for` `(``int` `i = 0; i < str.Length; i++) {``            ``if` `(str[i] == ``' '``) {``                ``list.Add(temp);``                ``temp = ``""``;``            ``}``            ``else` `{``                ``temp += str[i];``            ``}``        ``}``        ``list.Add(temp);``        ``return` `list;``    ``}` `    ``// Function to sort the given list based on values at``    ``// odd indices of the list``    ``static` `string` `SortListByOddIndices(List<``string``> list,``                                       ``int` `x)``    ``{``        ``int` `l = list.Count;` `        ``// Function to remove the values less than the given``        ``// value x``        ``for` `(``int` `i = l - 1; i > 0; i -= 2) {``            ``if` `(Int32.Parse(list[i]) < x) {``                ``list.RemoveRange(i - 1, 2);``            ``}``        ``}` `        ``l = list.Count;` `        ``for` `(``int` `i = 1; i < l; i += 2) {``            ``for` `(``int` `j = 1; j < l - i; j += 2) {``                ``// Compares values at odd indices of the``                ``// list to sort the list``                ``if` `(list[j] == list[j + 2]) {``                    ``if` `(list[j - 1].Length > list[j + 1].Length) {``                        ``string` `temp1 = list[j];``                        ``string` `temp2 = list[j - 1];``                        ``list[j] = list[j + 2];``                        ``list[j - 1] = list[j + 1];``                        ``list[j + 2] = temp1;``                        ``list[j + 1] = temp2;``                    ``}``                ``}``                ``else` `if` `(list[j].Length < list[j + 2].Length) {``                    ``string` `temp1 = list[j];``                    ``string` `temp2 = list[j - 1];``                    ``list[j] = list[j + 2];``                    ``list[j - 1] = list[j + 1];``                    ``list[j + 2] = temp1;``                    ``list[j + 1] = temp2;``                ``}``            ``}``        ``}` `        ``string` `sortedList = ``string``.Join(``" "``, list);` `        ``return` `sortedList;``    ``}` `    ``// Driver code` `    ``static` `public` `void` `Main (){``        ``string` `str = ``"Axc 78 Czy 60"``;``        ``int` `x = 77;` `        ``// Function call``        ``List<``string``> list = Tokenizer(str);` `        ``string` `sortedList = SortListByOddIndices(list, x);` `        ``Console.WriteLine(sortedList);``        ``Console.ReadLine();``    ``}``}``// contributed by akashish__`

## Java

 `// Java code for the above approach` `import` `java.util.ArrayList;``import` `java.util.List;` `public` `class` `Main {` `    ``// Function to split the input``    ``// string into a list``    ``public` `static` `List tokenizer(String Str) {``        ``List List = ``new` `ArrayList<>();``        ``for` `(String s : Str.split(``"\\s+"``)) {``            ``List.add(s);``        ``}``        ``return` `List;``    ``}` `    ``// Function to sort the given list based``    ``// on values at odd indices of the list``    ``public` `static` `String SortListByOddIndices(List List, ``int` `x) {` `        ``int` `l = List.size();` `        ``// Function to remove the values``        ``// less than the given value x``        ``for` `(``int` `i = l - ``1``; i >= ``0``; i -= ``2``) {``            ``if` `(Integer.parseInt(List.get(i)) < x) {``                ``List.remove(i - ``1``);``                ``List.remove(i - ``1``);``            ``}``        ``}` `        ``l = List.size();` `        ``for` `(``int` `i = ``1``; i < l; i += ``2``) {``            ``for` `(``int` `j = ``1``; j < l - i; j += ``2``) {` `                ``// Compares values at odd indices of``                ``// the list to sort the list``                ``if` `(List.get(j).compareTo(List.get(j + ``2``)) < ``0``                        ``|| (List.get(j - ``1``).compareTo(List.get(j + ``1``)) > ``0``                        ``&& List.get(j).equals(List.get(j + ``2``)))) {` `                    ``String temp1 = List.get(j);``                    ``String temp2 = List.get(j - ``1``);` `                    ``List.set(j, List.get(j + ``2``));``                    ``List.set(j + ``2``, temp1);` `                    ``List.set(j - ``1``, List.get(j + ``1``));``                    ``List.set(j + ``1``, temp2);``                ``}``            ``}``        ``}` `        ``return` `String.join(``" "``, List);``    ``}` `    ``// Driver Code``    ``public` `static` `void` `main(String[] args) {` `        ``String Str = ``"Axc 78 Czy 60"``;``        ``int` `x = ``77``;` `        ``// Function call``        ``List List = tokenizer(Str);` `        ``Str = SortListByOddIndices(List, x);` `        ``System.out.println(Str);``    ``}``}` `// This code is contributed by princekumaras`

Output:
`Axc 78`

Time Complexity: O(N2
Auxiliary Space: O(N)

Previous
Next