• Register
search
Log In
3 votes
21 views

Problem:

I am struggling with a problem for a few hours back. I know how to sort an array by using methods. I was looking for another way (perhaps a loop) to do it more efficiently. Let me put my question this way, how to sort an array in java without using sort method? Could anybody here please help to make this happen?

Thanks in advance

 

15.2k points

2 Answers

1 vote

Solution:

To mitigate the method you can use the iteration process to sort an array. This program takes a little more time time to execute. Moreover, this algorithm is not so efficient. So, my advice would be, use a sort function to sorting an array.

As per your question, I am trying to giving you a possible solution below:

public class ArraySort {    
    public static void main(String[] args) {        
            
        int [] array = new int [] {5, 2, 8, 7, 1};     
        int tem = 0;    
       
        for (int n = 0; n < array.length; n++) {     
            for (int m = n+1; m < array.length; m++) {     
               if(arr[n] > array[m]) {    
                   tem = array[n];    
                   array[n] = array[m];    
                   array[m] = tem;    
               }     
            }     
        }    
          
        System.out.println();    
            
        System.out.println("Elements of array sorted in ascending order: ");    
        for (int n = 0; n < array.length; n++) {     
            System.out.print(array[n] + " ");    
        }    
    }    
}    

Here I used a temporary variable named tem to make an empty space and sorting my elements.

The above program will sort your array in ascending order and provide an output:

Elements of array sorted in ascending order:
1 2 5 7 8 

Thanks

87.3k points
edited by
0 votes

Many predefined functions make your work easier but sometimes we want to do without using them.

Procedure:

To sort the array without using the sort algorithm, take the array of strings and sort the array based on the first string. Once you have done, then take the array of strings again and sort it based on the second character. Then again based on the third character and so on until you reach the length of the largest string.

Example:

This example will sort array elements in ascending and descending order both without using array sort method;

int a[]={6,2,5,1};
System.out.println(Arrays.toString(a));

int temp;
for(int i=0;i<a.length-1;i++){

   for(int j=0;j<a.length-1;j++){

       if(a[j] > a[j+1]){   // use < for Descending order

           temp = a[j+1];

            a[j+1] = a[j];

            a[j]=temp;

        }

     }

 }

System.out.println(Arrays.toString(a));

Output: 

 [6, 2, 5, 1]    // output for ascending order

 [1, 2, 5, 6]  //output for descending order

Second Example:

In this example we sort the array by using bubble sort.

public static void main(String[] args) {

 int[] arr = new int[] { 6, 8, 7, 4, 312, 78, 54, 9, 12, 100, 89, 74 };
 for (int i = 0; i < arr.length; i++) {

   for (int j = i + 1; j < arr.length; j++) {

      int tmp = 0;

      if (arr[i] > arr[j]) {

          tmp = arr[i];

          arr[i] = arr[j];

          arr[j] = tmp;

     }

   }

 }

}

 

3.9k points

Related questions

1 vote
1 answer 14 views
14 views
Problem: Hello Kodlogs, I find this website very helpful besides the StackOverflow. You guys are really awesome and friendly. I want to know from you guys, how to pass an array to a method? Also what happens when an array is passed to a method? Thanks for your valuable and patient answer.
asked Mar 31 Gavin 15.2k points
1 vote
1 answer 11 views
11 views
Problem: Hello Kodlogs, I have got a question from my fellow (java learner) friend and the question is: find the largest number in an array java I know about the array and I can print all the values sequentially. Moreover, I can add two different values of an array in ... Could any of you please give me a proper solution and tell me how to find the largest number in an array java? Thanks to all.
asked Mar 29 Gavin 15.2k points
3 votes
1 answer 18 views
18 views
Problem: Hello good souls, I am a new student of Java. I just started learning and trying to solve java problems. Right now, I am working with the arrays. I can do some basic things with an array. I am finding something more advance problems of an array. Thus I thought to find the ... in java? Let&rsquo;s say I have an array Integer[] num = { 2, 4, 6, 5, 8 }; Thanks in advance for your effort.
asked Mar 23 Gavin 15.2k points
0 votes
1 answer 17 views
17 views
Problem: Hello kodlogs, I have started learning python it been a month but I have a doubt that how can I be able to sort in python without using sort function and what are the possible ways to do this, though its been easy question, in c or c++ in college, we use to use sorting algorithm then it get sort is that same algorithm we can use in python too?
asked Jun 5 Gavin 15.2k points
1 vote
1 answer 15 views
15 views
Problem: Hello guys, I have a very simple question regarding Java Programming. I&rsquo;ve searched your website before posting this question but I haven't got any appropriate answer. Yes, there are a couple of answers but not as specific as I needed. I want to generate some random numbers in an array of Java programs. So, help me to fill an array with random numbers java. Many thanks...
asked Apr 6 Gavin 15.2k points