Solve Java Selection Sort Issues | Hi, I'm Working on Code

  • Java
  • Thread starter tshet
  • Start date
  • Tags
    Java Sort
In summary, the conversation revolves around the problem the person is facing with their selection sort code. They mention that they have tried to implement the structure of bubble sort into their selection sort but are still encountering errors. They also mention that the code does not compile and ask for help in identifying the problem. The other person asks for more information about the errors and suggests providing any information that would help in finding a solution.
  • #1
tshet
2
0
Hi, I'm currently working on my selection sort code, and I've come across some problems.

Java:
i
import java.util.Arrays;
import java.io.*;

public class SelectionSort
{
int a[];
static int n;

public static void main(String[] args)
{
int array[] = { 5,3,9,7,1,8 };
n = 6;
System.out.println(""+Arrays.toString(Sort(array)));
}

public static int[] Sort(int[] arr)
{
int t, min;
for(int i=0;i<n-1;i++)
{
min = i;
for(int j=i+1;j<n;j++)
{
if(a[min]>a[j])
min = j;
}
if(min!=i)
{
t = a[min];
a[min] = a;
a = t;
}
System.out.println(""+Arrays.toString(arr));
}
return arr;
}

}

Can someone please tell me what the problem is?
 
Last edited by a moderator:
Technology news on Phys.org
  • #2
What have you tried so far and what do you think is wrong?
 
  • #3
Well, I've taken the structure of my bubble sort and put it into my selection sort. I'm just trying to change the code for the actual selection part. I'm just getting simple mistakes now..
 
  • #4
It's doubt that it's that easy. I put the code in my IDE and it highlights several problems such that it won't compile. What's the error that you're getting when you try to compile?
 
  • #5
tshet said:
Can someone please tell me what the problem is?
It's difficult for us to say what the problem is merely by looking at your code. Make it easier by giving us a clue about what you're seeing? Are you getting compile errors? (Another poster in this thread said that your code doesn't compile.) If it doesn't compile, what error message is the compiler giving you?

If the code compiles but throws and exception or otherwise doesn't behave like it should, give us that information, and in fact, any information that would help us help you.
 

1. What is selection sort in Java?

Selection sort is a sorting algorithm in Java that works by repeatedly finding the minimum element from an unsorted list and placing it at the beginning of the list. This process is repeated until the entire list is sorted.

2. How does selection sort work in Java?

In selection sort, we divide the input list into two parts: the sorted sublist and the unsorted sublist. Initially, the sorted sublist is empty and the unsorted sublist is the entire input list. Then, we find the minimum element in the unsorted sublist and swap it with the first element of the unsorted sublist. This process is repeated until the entire list is sorted.

3. What are the advantages of using selection sort in Java?

Selection sort has a simple implementation and requires minimal extra memory, making it efficient for sorting smaller lists. It also has a time complexity of O(n^2), which is better than other simple sorting algorithms like bubble sort and insertion sort.

4. What are the common issues encountered while implementing selection sort in Java?

Some common issues with selection sort in Java include incorrect implementation of the sorting logic, not handling edge cases properly, and not properly initializing the array or list to be sorted.

5. How can I optimize selection sort in Java for better performance?

To optimize selection sort in Java, you can implement the algorithm to only swap elements when necessary, instead of swapping every time the minimum element is found. You can also use a different sorting algorithm, such as merge sort or quicksort, which have better time complexities for larger lists.

Similar threads

  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
29
Views
1K
  • Programming and Computer Science
Replies
3
Views
732
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
7
Views
2K
  • Programming and Computer Science
Replies
3
Views
2K
Back
Top