Selection Sort Not Working - Missing logic - java

Using selection sort to sort an array. I think my logic is right but there's this stupid error.
public static void arraySort(int[] a) {
//for loop to go through array
for(int i = 0; i < a.length; i++) {
int temp = a[i]; //set a temp value for first value
for (int x = i + 1; x < a.length; x++) {
if (a[x] < temp) {
a[i] = a[x];
temp = a[x];
}
}
}
}
At the end it keeps printing only one group of values repeatedly.

You should track the minimum index in the outer loop and swap two elements after each inner loop completion, e.g.:
for (i = 0; i < size - 1; i++) {
minIndex = i;
for (j = i; j < size; j++) {
if (array[j] < array[minIndex]) {
minIndex = j;
}
}
swap(array, i, minIndex);
}
Your fixed code:
public static void arraySort(int[] a) {
//for loop to go through array
for(int i = 0; i < a.length; i++) {
int temp = a[i]; //set a temp value for first value
int minIndex = i;
for (int x = i + 1; x < a.length; x++) {
if (a[x] < temp) {
a[i] = a[x];
temp = a[x];
minIndex = x;
}
}
//swap
a[minIndex] = a[i];
a[i] = temp;
}
}

Related

Difficulty trying to sort 10 numbers inputted by a user. Must use arrays and a separate method for sorting

My program isn't sorting the numbers at all. It displays them in the order they were initially entered. It must sort them from smallest to largest number. The code below should find the largest number in the array and swap it with the last .the code is below:
import java.util.Scanner;
public class maxSorttt {
public static void main(String[] args) {
double[] ten = new double[10];
Scanner input = new Scanner(System.in);
System.out.print("Enter 10 numbers: ");
for (int i = 0; i < ten.length; i++)
ten[i] = input.nextDouble();
sort(ten);
}
public static void sort(double[] array) {
for (int i = array.length - 1; i < 0; i--) {
double currentMax = array[i];
int currentMaxIndex = i;
for (int x = i - 1; x < -1; x--) {
if (currentMax < array[x]) {
currentMax = array[x];
currentMaxIndex = x;
}
}
if (currentMaxIndex != i) {
array[currentMaxIndex] = array[i];
array[i] = currentMax;
}
}
for (int i = 0; i < array.length; i++)
System.out.print(array[i] + " ");
}
}
I believe your problem is here:
for(int i=array.length-1; i<0; i--)
array.length is not less than 0 so the for loop never runs. You probably wanted
for(int i=array.length-1; i>=0; i--)
Be Simple!
public static void selectionSort(double[] arr) {
for (int i = 0; i + 1 < arr.length; i++) {
int minIndex = findMinIndex(arr, i + 1);
if (Double.compare(arr[i], arr[minIndex]) > 0)
swap(arr, i, minIndex);
}
}
private static int findMinIndex(double[] arr, int i) {
int minIndex = i;
for (; i < arr.length; i++)
if (Double.compare(arr[i], arr[minIndex]) < 0)
minIndex = i;
return minIndex;
}
private static void swap(double[] arr, int i, int j) {
double tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}

Insertion sort array in java

So I made this sortion sort method, all using for loops.
I revised it on white board over and over, it looks perfect, however, when I implemented it, it keeps giving me wrong sort, but if I reversed the if condition, it will give me the right answer but in reverse, this doesn't make sense!
public void insertionSort(){
for (int i = 1; i < items.length; i++){
for (int j = 0; j < i; j++){
if (items[i] < items[j]) {
int temp = items[i];
shift(items,j, i-1);
items[j] = temp;
}
}
}
}
private void shift(int[] array, int index1, int index2){
if (index1 == index2)
array[index1 + 1] = array[index1];
else{
for (int i = index2; i >= index1; i--)
array[i+1] = array[i];
}
}
Thanks for the input, I discovered the problem in my Array class, it simply doubles the array size, my mistake was that I used the array.length instead of count.
public void insertionSort(){
for (int i = 1; i < count; i++){
for (int j = 0; j < i; j++){
if (items[i] < items[j]) {
int temp = items[i];
shift(items,j, i-1);
items[j] = temp;
}
}
}
}

Shell sort is not sorting the first element of an array

When my sort is run on this data {7,8,4,2,3,9,5,8,4,1} only the first element is not put in its correct place. How can I fix this? Thanks for the help.
public void segmentedInsertionSort(int[] array, int size, int h)
{
int temp;
for(int i = h + 1 ;i < size;i++)
{
int j = i - h;
while(j > 0)
{
if(array[j+h] < array[j])
{
temp = array[j];
array[j] = array[j+h];
array[j+h] = temp;
j = j - h;
}
else
{
j = 0;
}
}
}
}
public void shellSort(int[] array, int size)
{
int h = size/2;
while(h > 0)
{
segmentedInsertionSort(array,size,h);
h = h/2;
}
}
for(int i = h + 1 ;i < size;i++)
{
int j = i - h;
while(j > 0)
{
if(array[j+h] < array[j])
{
temp = array[j];
array[j] = array[j+h];
array[j+h] = temp;
j = j - h;
}
In this part, you define i = h + 1 and then increase i value. So, so the j value is never less than 1 when the sort runs. Therefore, it never processes the first element of array. You need to fix this part.
I think you have misvalued some variables.Your variable j never reaches index 0 so as to compare 7 with any other value in this Comparison Sort.
Change:
for(int i = h +1 ;i < size;i++) to for(int i = h ;i < size;i++)
And
while(j > 0) to while(j >= 0)
else
{
j = 0;
}
to
else{
j = -1;
}
Final Code looks like:
` void segmentedInsertionSort(int arr[], int size, int h)
{
int temp;
for(int i = h ;i < size;i++)
{
int j = i-h ;
while(j >= 0)
{
if(arr[j+h] < arr[j])
{
temp = arr[j];
arr[j] = arr[j+h];
arr[j+h] = temp;
j = j - h;
}
else
{
j = -1;
}
}
}
}
void shellSort(int arr[], int size)
{
int h = size/2;
while(h > 0)
{
print(arr);
segmentedInsertionSort(arr,size,h);
h = h/2;
}
}

Maximum number from an array which is equal to product of two other numbers in that array

Recently, I took Linkedin placement test in which there was a question in which output for 4 test cases were wrong for me. I could not figure out what was my mistake becasue inputs/outputs were hidden.
Anyways here was the question:
Find the maximum element from an array where product of any other two elements would be equal to that number and return that number .If no, such element is there then return -1.
Here was my solution:
static int maxElement(int[] arr) {
Arrays.sort(arr);
int max = arr[arr.length-1];
int result = 0;
for (int i = 0; i < arr.length - 1; i++) {
for (int j = i + 1; j < arr.length; j++) {
result = arr[i] * arr[j];
if (result == max) {
return max;
}
}
}
return -1;
}
I guess you need to find the possible maximum number in the array and the product of two elements in the array.
If I assume this, your code fails for this test case:
int[] arr = {2,4,5,3,7,6}; , where the answer should be 6
Check this below code it will work for above test-case.
Just add one more reverse for loop to check the possible value and product.
static int maxElement(int[] arr) {
Arrays.sort(arr);
for (int k = arr.length-1; k >= 0; k--) {
int max = arr[k];
int result = 0;
for (int i = 0; i < arr.length - 1; i++) {
for (int j = i + 1; j < arr.length; j++) {
result = arr[i] * arr[j];
if (result == max) {
return max;
}
}
}
}
return -1;
}
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < a.length; i++) {
list.add(a[i]);
}
int maxSum = 0;
for (int i = 0; i < a.length; i++) {
for (int j = i + 1; j < a.length; j++) {
if ((a[i] * a[j]) > maxSum) {
if(list.contains(a[i] * a[j]))
maxSum = a[i] * a[j];
}
}
}
if (maxSum != 0)
return maxSum;
return -1;

How to implement selection sort?

I have the following code snippet..
public void selsort()
{
int j=0,i,k; int low;
for(k = 0; k < ele; k++)
{
for(i = j;i < ele; i++)
if(a[i] < a[i+1])
low = i;
if(i!=j)
{
long temp=a[j];
a[j]=a[i];
a[i]=temp;
}
j++;
}
}
I don't know why the above algorithm is not working properly.
What has to be modified to get it working properly?
fixed your code
public void selsort() {
int j = 0, i, k;
int low;
for (k = 0; k < ele; k++) {
low = j;
for (i = j; i < ele; i++)
if (a[i] < a[low]) {
low = i;
}
if (j != low) {
int temp = a[j];
a[j] = a[low];
a[low] = temp;
}
j++;
}
}
but can be used k insdead of j, and better have input params instead using class fields like a and ele

Categories