Hi All,
I think we can sort the following way. Not sure about the complexity.
public class SingleLoopSorting {
public static void main(
String[] args) {
int arr[] = { 5, 1, 7, 3, 9 };
for (int i = 1; i < arr.length; i++) {
if (arr[i] < arr[i - 1]) {
arr[i] = arr[i] + arr[i - 1];
arr[i - 1] = arr[i] - arr[i - 1];
arr[i] = arr[i] - arr[i - 1];
i = 0;
}
}
System.out.print("Sorted Array : ");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}