Java : Sort integer array without using Arrays.sort()

161,945

Solution 1

You can find so many different sorting algorithms in internet, but if you want to fix your own solution you can do following changes in your code:

Instead of:

 orderedNums[greater]=tenNums[indexL];

you need to do this:

while (orderedNums[greater] == tenNums[indexL]) {
     greater++;
}
orderedNums[greater] = tenNums[indexL];

This code basically checks if that particular index is occupied by a similar number, then it will try to find next free index.

Note: Since the default value in your sorted array elements is 0, you need to make sure 0 is not in your list. otherwise you need to initiate your sorted array with an especial number that you sure is not in your list e.g: Integer.MAX_VALUE

Solution 2

Simple sorting algorithm 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;
            }
        }
    }
}

Solution 3

Simple way :

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]
    [1, 2, 5, 6]

Solution 4

Here is one simple solution

public static void main(String[] args) {        
        //Without using Arrays.sort function
        int i;
        int nos[] = {12,9,-4,-1,3,10,34,12,11};
        System.out.print("Values before sorting: \n");
        for(i = 0; i < nos.length; i++)
            System.out.println( nos[i]+"  ");               
        sort(nos, nos.length);
        System.out.print("Values after sorting: \n");       
        for(i = 0; i <nos.length; i++){
            System.out.println(nos[i]+"  ");
        }
    }

    private static void sort(int nos[], int n) {
     for (int i = 1; i < n; i++){
          int j = i;
          int B = nos[i];
          while ((j > 0) && (nos[j-1] > B)){
            nos[j] = nos[j-1];
            j--;
          }
          nos[j] = B;
        }
    }

And the output is:

Values before sorting:

12
9
-4
-1
3
10
34
12
11

Values after sorting:

-4
-1
3
9
10
11
12
12
34

Solution 5

int x[] = { 10, 30, 15, 69, 52, 89, 5 };
    int max, temp = 0, index = 0;
    for (int i = 0; i < x.length; i++) {
        int counter = 0;
        max = x[i];
        for (int j = i + 1; j < x.length; j++) {

            if (x[j] > max) {
                max = x[j];
                index = j;
                counter++;
            }

        }
        if (counter > 0) {
            temp = x[index];
            x[index] = x[i];
            x[i] = temp;
        }
    }
    for (int i = 0; i < x.length; i++) {
        System.out.println(x[i]);
    }
Share:
161,945
ransan32
Author by

ransan32

please delete me

Updated on July 09, 2022

Comments

  • ransan32
    ransan32 almost 2 years

    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 problem out of all the other exercises because I've been struggling to find the 'perfect algorithm' for this.

    Write JAVA program that will input 10 integer values and display either in ascending or descending order. Note: Arrays.sort() is not allowed.

    This is the code I have come up with, it works but it has one obvious flaw. If I enter the same value twice or more, for example:

    5, 5, 5, 4, 6, 7, 3, 2, 8, 10

    Only one of the three 5s entered would be counted and included in the output. The output I get (for the ascending order) is:

    2 3 4 5 0 0 6 7 8 10.

    import java.util.Scanner;
    
    public class Exer3AscDesc
    {
        public static void main(String args[])
        {
            Scanner scan = new Scanner(System.in);
            int tenNums[]=new int[10], orderedNums[]=new int[10];
            int greater;
            String choice;
    
            //get input
            System.out.println("Enter 10 integers : ");
            for (int i=0;i<tenNums.length;i++)
            {
                System.out.print(i+1+"=> ");
                tenNums[i] = scan.nextInt();
            }
            System.out.println();
    
            //imperfect number ordering algorithm
            for(int indexL=0;indexL<tenNums.length;indexL++)
            {
                greater=0;
                for(int indexR=0;indexR<tenNums.length;indexR++)
                {
                    if(tenNums[indexL]>tenNums[indexR])
                    {
                        greater++;
                    }
                }
                orderedNums[greater]=tenNums[indexL];
            }
    
            //ask if ascending or descending
            System.out.print("Display order :\nA - Ascending\nD - Descending\nEnter your choice : ");
            choice = scan.next();
    
            //output the numbers based on choice
            if(choice.equalsIgnoreCase("a"))
            {
                for(greater=0;greater<orderedNums.length;greater++)
                {
                    System.out.print(orderedNums[greater]+" ");
                }
            }
            else if(choice.equalsIgnoreCase("d"))
            {
                for(greater=9;greater>-1;greater--)
                {
                    System.out.print(orderedNums[greater]+" ");
                }
            }
        }
    }