Java: sort an integer array without using Array.sort ()

This is an instruction in one of the exercises in our Java class. First of all, I would like to say that I am “doing my homework”, and I'm not just lazy, asking someone from Qaru to answer this question for me. This particular element was my problem from all the other exercises, because I struggled to find the “perfect algorithm” for this.

Write a JAVA program that will enter 10 integer values ​​and display them in ascending or descending order. Note. Arrays .sort () are not allowed.

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

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

Only one of the three entered 5s will be counted and included in the output. The output I get (for upstream):

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]+" "); } } } } 
+7
source share
11 answers

You can find so many different sorting algorithms on the Internet, but if you want to fix your decision, you can make the following changes to your code:

Instead:

  orderedNums[greater]=tenNums[indexL]; 

you need to do this:

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

This code basically checks to see if this particular index is occupied by a similar number, then tries to find the next free index.

Note. Since the default value in your sorted array elements is 0, you need to make sure that 0 is not in your list. otherwise, you need to run a sorted array with a special number that you are not sure about your list, for example: Integer.MAX_VALUE

+4
source

Simple sorting algorithm Sorting bubbles :

 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; } } } } 
+14
source

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 result:

Values ​​before sorting:

12
nine
-4
-one
3
10
34
12
eleven

Values ​​after sorting:

-4
-one
3
nine
10
eleven
12
12
34

+2
source

I would recommend looking at Sort Sort or Insert Sort if you are not too worried about performance. Maybe this will give you some ideas.

+1
source

A 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] 
+1
source

Here you can use the bubble sort:

  //Time complexity: O(n^2) public static int[] bubbleSort(final int[] arr) { if (arr == null || arr.length <= 1) { return arr; } for (int i = 0; i < arr.length; i++) { for (int j = 1; j < arr.length - i; j++) { if (arr[j - 1] > arr[j]) { arr[j] = arr[j] + arr[j - 1]; arr[j - 1] = arr[j] - arr[j - 1]; arr[j] = arr[j] - arr[j - 1]; } } } return arr; } 
0
source

Sorting an array without using the built-in functions in java ...... just create a new file by canceling this name → (ArraySorting.java) ..... Run the project and enjoy it !!!!!

  import java.io.*; import java.util.Arrays; import java.util.Scanner; public class ArraySorting { public static void main(String args[]) { int temp=0; Scanner user_input=new Scanner(System.in); System.out.println("enter Size of Array..."); int Size=user_input.nextInt(); int[] a=new int[Size]; System.out.println("Enter element Of an Array..."); for(int j=0;j<Size;j++) { a[j]=user_input.nextInt(); } for(int index=0;index<a.length;index++) { for(int j=index+1;j<a.length;j++) { if(a[index] > a[j] ) { temp = a[index]; a[index] = a[j]; a[j] = temp; } } } System.out.print("Output is:- "); for(int i=0;i<a.length;i++) { System.out.println(a[i]); } } } 
0
source
 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]); } 
0
source
 class Sort { public static void main(String[] args) { System.out.println("Enter the range"); java.util.Scanner sc=new java.util.Scanner(System.in); int n=sc.nextInt(); int arr[]=new int[n]; System.out.println("Enter the array values"); for(int i=0;i<=n-1;i++) { arr[i]=sc.nextInt(); } System.out.println("Before sorting array values are"); for(int i=0;i<=n-1;i++) { System.out.println(arr[i]); } System.out.println(); for(int pass=1;pass<=n;pass++) { for(int i=0;i<=n-1;i++) { if(i==n-1) { break; } int temp; if(arr[i]>arr[i+1]) { temp=arr[i]; arr[i]=arr[i+1]; arr[i+1]=temp; } } } System.out.println("After sorting array values are"); for(int i=0;i<=n-1;i++) { System.out.println(arr[i]); } } } 
-one
source

Here is a sorting example. A simple example.

 public class SortingSimpleExample { public static void main(String[] args) { int[] a={10,20,1,5,4,20,6,4,2,5,4,6,8,-5,-1}; a=sort(a); for(int i:a) System.out.println(i); } public static int[] sort(int[] a){ for(int i=0;i<a.length;i++){ for(int j=0;j<a.length;j++){ int temp=0; if(a[i]<a[j]){ temp=a[j]; a[j]=a[i]; a[i]=temp; } } } return a; } } 
-one
source

This will help you.

 int n[] = {4,6,9,1,7}; for(int i=n.length;i>=0;i--){ for(int j=0;j<n.length-1;j++){ if(n[j] > n[j+1]){ swapNumbers(j,j+1,n); } } } printNumbers(n); } private static void swapNumbers(int i, int j, int[] array) { int temp; temp = array[i]; array[i] = array[j]; array[j] = temp; } private static void printNumbers(int[] input) { for (int i = 0; i < input.length; i++) { System.out.print(input[i] + ", "); } System.out.println("\n"); } 
-2
source

All Articles