• Register
3 votes
3.8k 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 63 views
0 votes
1 answer 33 views
33 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 25 views
0 votes
1 answer 32 views
32 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 26 views
26 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 5.3k points
0 votes
1 answer 23 views
23 views
Problem: I have fundamental knowledge of Java. I am trying to arrange the elements from an array and it is strictly based on the number of occurrences of that particular value in the ascending order in a java. This is what I have already tried: int a[]={0,0,0,1,3,3,2,1,3,5,6,0}; int b=a.length; for ... temp=a[i]; a[i]=a[j]; a[j]=temp; } } } for(int r=0;r<a.length;r++) { System.out.println(a[r]); }
asked Sep 5, 2020 Raphael Pacheco 4.9k points
1 vote
1 answer 22 views
22 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, 2020 Gavin 15.3k points
0 votes
1 answer 27 views
27 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, 2020 Gavin 15.3k points
1 vote
1 answer 20 views
20 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, 2020 Gavin 15.3k points