How to sort an array in a single loop ? | C ++ code to sort an array of Strings with the help of single loop ; Function for Sorting the array using a single loop ; Sorting using a single loop ; Type Conversion of char to int . ; Comparing the ascii code . ; Swapping of the characters ; Driver code ; Declaring a String ; declaring character array ; copying the contents of the string to char array ; Printing the original Array . ; Sorting the array using a single loop ; Printing the sorted array .
#include <bits/stdc++.h> NEW_LINE using namespace std ; char * sortArrays ( char arr [ ] , int length ) { for ( int j = 0 ; j < length - 1 ; j ++ ) { int d1 = arr [ j ] ; int d2 = arr [ j + 1 ] ; if ( d1 > d2 ) { char temp = arr [ j ] ; arr [ j ] = arr [ j + 1 ] ; arr [ j + 1 ] = temp ; j = -1 ; } } return arr ; } int main ( ) { string geeks = " GEEKSFORGEEKS " ; int n = geeks . length ( ) ; char arr [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { arr [ i ] = geeks [ i ] ; } cout << " Original ▁ array : ▁ [ " ; for ( int i = 0 ; i < n ; i ++ ) { cout << arr [ i ] ; if ( i + 1 != n ) cout << " , ▁ " ; } cout << " ] " << endl ; char * ansarr ; ansarr = sortArrays ( arr , n ) ; cout << " Sorted ▁ array : ▁ [ " ; for ( int i = 0 ; i < n ; i ++ ) { cout << ansarr [ i ] ; if ( i + 1 != n ) cout << " , ▁ " ; } cout << " ] " << endl ; }