• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Can we sort using a single loop?

 
Ranch Hand
Posts: 112
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I want to know if this is possible:" Writing a program that sorts an array by using only a single loop."; I dont want the solution but I wanted to know if this is really feasible.

Thanks,
Pavan.
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


where 'x' is the integer array name, 'n' is the number of elements in 'x'.
This for loop sorts the elements of the array in ascending order. To sort in descending order, change "if ( x[ i ] < x[ i + 1 ] )" to "if ( x[ i ] > x[ i + 1 ] )"

I have no way of testing all of these, but I'm pretty sure they'll work.
 
Pavan Kumar Dittakavi
Ranch Hand
Posts: 112
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I have got it.


@anirudh jagithyala: The code you provided fails for input 5 1 7 3 9. Thanks anyway.

Thanks,
-Pavan.
 
Bartender
Posts: 1205
22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pavan Kumar Dittakavi wrote:Hi All,

I want to know if this is possible:" Writing a program that sorts an array by using only a single loop."; I dont want the solution but I wanted to know if this is really feasible.

Thanks,
Pavan.



Short answer: Any algorithm can be reworked as a single loop.

For this problems, you can implement what amounts to a bubble sort by using two indices (say i and j) and just have all the initialization, incrementing and termination expressions in a handful of if/then/else statements at the bottom of the single loop. It would still execute in O(n^2) time, but it would technically be a single loop.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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] + " ");
}
}
}
 
Bartender
Posts: 322
24
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Abhishek,

Welcome to the Ranch!

I imagine the OP from 3 years ago has moved on, but thank you for contributing.
In the future, please wrap your code using Code Tags (← click here if you are not sure how).

Code Tags will make your code much easier for everyone to read, as demonstrated here:

Cheers!
Chris
 
Abhishek K Sharma
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Chris,
Sure I will do that. I posted the reply thinking that it might be helpful to some other person.
 
reply
    Bookmark Topic Watch Topic
  • New Topic