
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Sort a String Without Using Predefined Methods in Java
The java.lang.String class represents an immutable sequence of characters and cannot be changed once created. We need to instantiate this class or assign values directly to its literal to create a string in Java.
The String class does not provide any built-in method to sort the contents of a string. To sort a String, we need to convert it into a character array using the toCharArray() method and sort the array. To sort a character array, we can either use the Arrays.sort() method or use sorting algorithms.
Since the given task is to sort a string without using any predefined methods, we can use sorting algorithms that help arrange the elements of a list or array in sorted order. Following are two sorting algorithms that will be used to sort a string:
Sorting a String using the Bubble Sort Algorithm
The Bubble Sort algorithm is a sorting technique used to sort the elements in an array. In the Bubble Sort algorithm, adjacent elements are compared and swapped if they are not in the correct order. This process repeats, "bubbling up" the largest element to the end of the array on each pass.
Following are the steps of the Bubble Sort algorithm:
- Step 1: Check if the first element in the input array is greater than the next element in the array.
- Step 2: If it is greater, swap the two elements; otherwise, move the pointer forward in the array.
- Step 3: Repeat Step 2 until we reach the end of the array.
- Step 4: Check if the elements are sorted; if not, repeat the same process (Step 1 to Step 3) from the last element of the array to the first.
- Step 5: The final output achieved is the sorted array.
Example
The following program uses the Bubble Sort algorithm to sort the given string 'Tutorialspoint':
public class sortString { public static void main(String[] args) { String str = "Tutorialspoint"; System.out.println("String before sorting: " + str); char[] chars = str.toCharArray(); // Bubble sort logic for (int i = 0; i <chars.length - 1; i++) { for (int j = 0; j < chars.length - i - 1; j++) { if (chars[j] > chars[j + 1]) { // Swap characters char temp = chars[j]; chars[j] = chars[j + 1]; chars[j + 1] = temp; } } } System.out.print("String after sorting: "); for(int i = 0; i<chars.length; i++){ System.out.print(chars[i]); } } }
The above program displays a sorted string:
String before sorting: Tutorialspoint String after sorting: Taiilnooprsttu
Using Selection Sort Algorithm
Selection Sort is a simple sorting algorithm. This algorithm, like insertion sort, is an in-place comparison-based algorithm in which the array is divided into two parts: the sorted part at the left end and the unsorted part at the right end. Initially, the sorted part is empty, and the unsorted part is the entire array.
Following are the steps of the Selection Sort algorithm:
- Step 1: Set MIN to location 0.
- Step 2: Search for the minimum element in the list.
- Step 3: Swap with value at location MIN.
- Step 4: Increment MIN to point to the next element.
- Step 5: Repeat until the list is sorted.
Example
This is another way of sorting the given string "Hello From TP" without using the predefined method. We use the Selection Sort algorithm to sort the given string:
public class sortString { public static void main(String[] args) { String str = "Hello from TP"; System.out.println("String before sorting: " + str); // Convert to char array char[] chars = str.toCharArray(); int n = chars.length; // Selection sort for (int i = 0; i < n - 1; i++) { int minIndex = i; for (int j = i + 1; j < n; j++) { if (chars[j] < chars[minIndex]) { minIndex = j; } } // Swap if a smaller element was found if (minIndex != i) { char temp = chars[i]; chars[i] = chars[minIndex]; chars[minIndex] = temp; } } String sortedStr = new String(chars); System.out.println("String after sorting:" + sortedStr); } }
Following is the output of the above program:
String before sorting: Hello from TP String after sorting: HPTefllmoor