• Register
3 votes
7.1k 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

 

12 7 7
15,250 points

Please log in or register to answer this question.

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

13 9 6
94,240 points
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;

     }

   }

 }

}

 

11 5 2
3,890 points

Related questions

0 votes
1 answer 25 views
25 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
asked Mar 6 Wafa Abu Yousef 6.1k points
0 votes
1 answer 127 views
0 votes
1 answer 52 views
52 views
I want to know how to sort an array in java without using sort method.
asked Sep 28, 2020 Daniel Anderson 4k points
0 votes
1 answer 34 views
0 votes
2 answers 4 views
4 views
Problem: I'm having problems during my learning as I mentioned in my question above, I try to find on web search but I can't get proper information, can you help me to continue my project?
asked Mar 29 tuhin1 47.2k points
0 votes
1 answer 55 views
55 views
We have obtain a full of data stored in tables via Mysql on which we have done few analysis and also export microsoft excel to create graphs and such however the customer demand it to be a desktop application.
asked Sep 26, 2020 Daniel Anderson 4k points
0 votes
1 answer 2 views
2 views
Problem: This is the instruction in one of the exercises in our Java class. Before anything else, I would like to say that I 'do my homework' and I'm not just being lazy asking someone on Stack Overflow to answer this for me. This specific item has been my ... of all the other exercises because I've been struggling to find the 'perfect algorithm' for this. Please help me to fix this. Thanks.
asked Apr 3 Shovo210 13.2k points
0 votes
1 answer 9 views
9 views
Problem: Please help me I am unable to find out the solution.
asked Mar 12 Ifra 24.4k points
0 votes
1 answer 31 views
31 views
C++ sort array of strings I'm trying to sort an array of strings, but it's not sorting.... what am I doing wrong? string namesS[MAX_NAMES]; int compare (const void * a, const void * b){      return (*(char*)a-*(char*)b ); } void sortNames(){      qsort(namesS, MAX_NAMES, sizeof(string), compare); }
asked Aug 29, 2020 sasha 16.2k points