Sorting Method Not Sorting Properly - java

This method is supposed to sort the words from a given file in alphabetical order after it is selected. Everything is working except it doesn't properly sort it. The input file reads "kundu is a man kundu man", but no matter what I try I get "[is, kundu, a, man, kundu, man]".
I tried taking away the "-1" and the "+1" but that did nothing to help.
private String[] selectionSort(String[] stringArray)
{
for(int j = 0; j < stringArray.length - 1; j++)
{
int min = j;
for(int k = j + 1; k < stringArray.length; k++)
{
if(stringArray[k].compareTo(stringArray[min]) < 0)
min = k;
swap(stringArray, j, min); //this method swaps the words
// by using a temp
//swap(intArray, j, min);
}
}
return stringArray;
}
private void swap(String [] stringArray, int i, int j) //swap method
{
String temp = stringArray[i];
stringArray[i] = stringArray [j];
stringArray[j] = temp;
}

Your swap call should be after the inner loop. Like,
private String[] selectionSort(String[] stringArray) {
for (int j = 0; j < stringArray.length - 1; j++) {
int min = j;
for (int k = j + 1; k < stringArray.length; k++) {
if (stringArray[k].compareTo(stringArray[min]) < 0) {
min = k;
}
}
swap(stringArray, j, min);
}
return stringArray;
}
After that, with no other changes and your input, I get
[a, is, kundu, kundu, man, man]

Related

java - Selection Sort

I want to implement a selection alignment that receives 10 integers and organizes them in ascending order.
However, when my code is operated, other things work normally, but only the first integer is not aligned.
Please let me know how to fix the code.
public static void sort(int[] array) {
Scanner sc = new Scanner(System.in);
System.out.println("put the int");
for (int i =0;i <array.length;i++) {
System.out.print((i+1)+": ");
int n = sc.nextInt();
array[i] = n;
for (int j = 1; j < array.length;j++) {
if (array[i] < array[j]) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
for (int a=0; a< array.length; a++) {
System.out.print(array[a]+" ");
}
}
public static void main(String[] args) {
int[] my_array = {0,0,0,0,0,0,0,0,0,0};
sort(my_array);
}
}
You should set
int j = 0
in the inner for
If you want to implement Selctionsort
initialize the inner for with int j = i+1
change numbers if array[i] is greater than array[j] not the other way around
for (int i =0;i <array.length;i++) {
int minValue = array[i];
for (int j = i +1; j < array.length;j++) {
if (array[i] > array[j]) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
for (int a=0; a< array.length; a++) {
System.out.print(array[a]+" ");
}
You need to read the entire array in first, then you can sort it. Also, I don't consider your algorithm a true selection sort. In selection sort, you must find the minimum in the array of unsorted data. Then you swap. Your algorithm doesn't do that exactly.
To illustrate, I have broken the code into functions.
// Find the minimum value in the array, starting the search at "start"
// Returns the index of the minimum
static int findMinIndex(int[] array, int start)
{
int min = array[start];
int minIndex = start;
for (int i = start + 1; i < array.length; i++) {
if (array[i] < min) {
min = array[i];
minIndex = i;
}
}
return minIndex;
}
// Swap 2 elements of an array
static void swap(int[] array, int index1, int index2)
{
int temp = array[index1];
array[index1] = array[index2];
array[index2] = temp;
}
// Selection sort the array, ascending
static void selectionSort(int[] array)
{
for (int i = 0; i < array.length; i++) {
// First find the minimum from i to the end of the array...
int minIndex = findMinIndex(array, i);
// ...then swap
if (minIndex != i) {
swap(array, i, minIndex);
}
}
}

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;
}
}
}
}

WAP to return the length of longest common substring between two strings

Two strings are given and we have to find the length of longest common substring. I don't know what's wrong with my code.
The outer loop takes a substring of B and the inner loop increases the substring by one character at a time.
For the input "www.lintcode.com code", "www.ninechapter.com code" the output is coming 5 but it should be 9
public class Solution {
/**
* #param A, B: Two string.
* #return: the length of the longest common substring.
*/
public int longestCommonSubstring(String A, String B) {
// write your code here
int k = 0, temp = 0;
if(B.length() == 0){
return 0;
}
for(int i = 0; i < B.length()-1; i++){
String bb = B.substring(i, i+1);
if(A.contains(bb)){
for(int j = 1; j < A.length()-i; j++){
String bbb = B.substring(i, i+j);
if(!A.contains(bbb))
break;
temp = bbb.length();
}
}
if(temp > k)
k = temp;
}
return k;
}
}
Just replace this:
for(int j = 1; j < A.length()-i; j++)
with this:
for(int j = 1; j < B.length()-i+1; j++)
I believe you could reduce your function size a little with this...not sure if your method is more efficient or not though...
public int longestCommonSubstring(String A, String B) {
int longestSubstring = 0;
for (int x=0; x < A.length(); x++) {
for (int y=x; y < A.length() + 1; y++) {
String testString = A.substring(x,y);
if (B.contains(testString) && (testString.length() > longestSubstring)) {
longestSubstring = testString.length();
}
}
}
return longestSubstring;
}

access array elements in java outside the loop

I am developing a simple program to print seat numbers, where the row are numbered from 1-5 and columns from a-e. the code i am using is as follows
public class JavaApplication5 {
public static void main(String[] args) {
int j =1,k;
int i;
char c;
String[] arr = new String[25];
for( i = 0;i < arr.length;i++)
{
while(j <= 5)
{
for(k = 97;k < 102; k++)
{
c = ((char)k);
arr[i] = j + "" + c;
System.out.println(arr[i]);
}
j++;
}
}
}
}
this displays desired result. but when I print an element outside the for loop I get the result as null like below
public static void main(String[] args) {
int j =1,k;
int i;
char c;
String[] arr = new String[25];
for( i = 0;i < arr.length;i++)
{
while(j <= 5)
{
for(k = 97;k < 102; k++)
{
c = ((char)k);
arr[i] = j + "" + c;
}
j++;
}
}
System.out.println(arr[6]);
}
how to solve this?
this will leave all elements as null
String[] arr = new String[25];
this will iterate until j == 5 so only until arr[5]
while(j <= 5) {
j++;
}
Therefore arr[6] is still null
Change
arr[j] = j + "" + c;
instead of
arr[i] = j + "" + c;
Now it works.
public static void main(String[] args) {
int j = 1, k;
int i;
char c;
String[] arr = new String[25];
for (i = 0; i < arr.length; i++) {
while (j <= 5) {
for (k = 97; k < 102; k++) {
c = ((char) k);
arr[j] = j + "" + c;
}
j++;
}
}
System.out.println(arr[1]);
System.out.println(arr[2]);
System.out.println(arr[3]);
System.out.println(arr[4]);
System.out.println(arr[5]);
System.out.println(arr[6]); // null because your check j <= 5 in while loop
}
You can access the array elements normally outside of the loop. In your example, arr[6] is just null. The fault is not in the way you access it. (Although i cant see the bug yet ;))
The problem in your code is that you simply write 5 time on index 1, then 5 time on index 2 and so on.
So you never wrote on index 6.
Your code should be changed to code below:
String[] arr = new String[25];
int i = 0;
int j = 1;
while (j <= 5) {
for (k = 97; k < 102; k++) {
c = ((char) k);
arr[i++] = j + "" + c;
}
j++;
}
System.out.println(arr[6]);
Because your loops run 5*5 times, then your i index will never pass arr array length.
But you better control it like this to prevent your code from being error prone:
if(i < arr.length) {
arr[i++] = j + "" + c;
} else {
break;
}

Categories