-5
public class sorting {
  public static void sort(int arr[])     {
        int n = arr.length;
        int count = 0;
        int tempArr [] = arr.clone();
 
        // One by one move boundary of unsorted subarray
        for (int i = 0; i < n-1; i++){
            // compare proceeding element
            int val = i;
            for (int j = 0; j < n; j++){
                if (arr[val] > arr[j])
                    count++;
                    System.out.println(count);
            }
            tempArr[count] = arr[val];
            count = 0;//reset counter
        }
      for (int i = 0; i < n; ++i)
            System.out.print(tempArr[i] + " ");
    }
   public static void main(String args[]) {
     
   int a[] = {50, 40, 30, 20, 10};
   sort(a);
   }
}

I assume that this would sort the arrays in another array, but there is a problem where index 0 won't change. This uses a method of comparing the current value to all the elements in the array. Then using count as the index by incrementing it if the current key is greater than the proceeding values.

The problem is that it increments at the last part.

0
1
2
3
4
0
0
1
2
3
0
0
0
1
2
0
0
0
0
1  //it incremented 
50 20 30 40 50 

karel
  • 5,489
  • 46
  • 45
  • 50
Elsa Zone
  • 1
  • 1
  • 2
    `if (arr[val] > arr[j]) {` --- you currently only have `count++;` in the `if` body because you have no braces. Java is not Python. – Elliott Frisch Jan 02 '22 at 07:43
  • it works just fine without the braces................................... and even if I place braces, it;s the same – Elsa Zone Jan 02 '22 at 08:17
  • @ElsaZone: No, it absolutely does *not* behave the same way with and without braces. Currently, `System.out.println(count);` is not part of the `if` body. If you put it in braces, it *will* be part of the body. Those are not the same. – Jon Skeet Jan 02 '22 at 09:45

1 Answers1

2

Your first loop start with 0 and second loop also start from 0 that is a problem but it should be 0 and 1. Change initialization of second loop j = 0 to j = i + 1. it can solve your problem.

Here down is modified code:

public class Main
{
    public static void sort(int arr[])
    {
        int n = arr.length;
        int count = 0;
        int temp;
        int tempArr [] = arr.clone();
 
        for (int i = 0; i < n; i++){
            for (int j = i + 1; j < n; j++){
                if (arr[i] > arr[j])
                {
                    count++;
                    temp = tempArr[i];
                    tempArr[i] = tempArr[j];
                    tempArr[j] = temp;
                    System.out.println(count);
                }
                else
                {
                    count = 0;       
                }
            }
        }
      for (int i = 0; i < n; ++i)
            System.out.print("\n" + tempArr[i] + " ");
    }
    public static void main(String args[]){
     
        int a[] = {20, 10, 40, 30, 60, 50, 80, 25, 70, 100};
        sort(a);
    }
}

If you wanna make your code easy then use java built-in method Arrays.sort(arrray):

import java.util.*;
public class Main
{
    public static void sort(int arr[])
    {
        Arrays.sort(arr);
        System.out.println(Arrays.toString(arr));
    }
    public static void main(String args[]){
     
        int a[] = {50, 40, 30, 20, 10};
        sort(a);
    }
}
Ryan M
  • 18,333
  • 31
  • 67
  • 74
Faheem azaz Bhanej
  • 2,328
  • 3
  • 14
  • 29