I am getting wrong output in SelectionSort - java

I was Supposed to get the output as
{-10,6,7,13,20,80,90,100}
but I am getting the output as
{20,100,13,6,7,80,-10,90}
in SelectionSort. Why am I getting this output? How can I solve this problem?
public class x{
public static void main(String args[]){
int[] a= {20,13,6,7,80,-10,90,100};
int i,j,c,largest;
for(i=a.length-1;i>0;i--){
largest=0;
for(j=1;j<=(a.length-1);j++){
if(a[j]>a[largest]){
largest=j;
}
}
swap(a,largest,i);
}
for(c=0;c<a.length;c++)
System.out.println(a[c]);
}
public static void swap(int[] arr, int a, int b){
int temp;
if(a==b){
return;
}
temp=arr[a];
arr[a]=arr[b];
arr[b]=temp;
}
}

public class x{
public static void main(String args[]){
int[] a= {20,13,6,7,80,-10,90,100};
int i,j,c,largest;
for(i=a.length-1;i>=0;i--){
largest=0;
for(j=1;j<=i;j++){
if(a[j]>a[largest]){
largest=j;
}
}
swap(a,largest,i);
}
for(c=0;c<a.length;c++)
System.out.println(a[c]);
}
public static void swap(int[] arr, int a, int b){
int temp;
if(a==b){
return;
}
temp=arr[a];
arr[a]=arr[b];
arr[b]=temp;
}
}

Related

what is the prolem in my code? it not compiled and get error.Please help me

This is the program for print the character of given index.But is gets error in function.
I give the function return type as char.But compiler told to rename the return type of main function
import java.util.Scanner;
class Demo5{
public static void main(String[] args)
{
Scanner n=new Scanner(System.in);
String p=n.next();
int q=n.nextInt();
System.out.println(showchar(p,q));
public char showchar(String s,int num)
{
char c=s.charAt(num);
return c;
}
}
}
public static void main(String[] args)
{
Scanner n=new Scanner(System.in);
String p=n.next();
int q=n.nextInt();
System.out.println(showchar(p,q));
}
public static char showchar(String s,int num)
{
char c=s.charAt(num);
return c;
}

i have problem understanding difference between two solutions

why should i use two swap functions in second and i only one swap function in first
//first one am getting correct answer with only one swap
import java.util.*;
public class stringper
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String s=sc.next();
perm(s,0,s.length());
}
static void perm(String s,int left,int right)
{
if(left==right)
{
System.out.print(s+" ");
}
for(int i=left;i<right;i++)
{
s=swap(s,i,left,1);
perm(s,left+1,right);
}
}
static String swap(String s,int i,int j)
{
char ch[]=s.toCharArray();
char temp=ch[i];
ch[i]=ch[j];
ch[j]=temp;
s=new String(ch);
return s;
}
}
//in the second code am getting correct answer with two swap functions
import java.util.*;
public class stringper
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String s=sc.next();
int nums[]={1,2,3};
perm(nums,0,nums.length);
}
static void perm(int[] nums,int left,int right)
{
for(int k=0;k<nums.length;k++)
{
System.out.print(nums[k]);
}
System.out.print(" ");
if(left==right)
{
ArrayList<Integer> l1=new ArrayList<Integer>();
for(int i=0;i<nums.length;i++)
{
//l1.add(nums[i]);
}
//l.add(l1);
}
for(int i=left;i<right;i++)
{
nums=swap(nums,i,left);
perm(nums,left+1,right);
nums=swap(nums,i,left);
}
}
static int[] swap(int[] nums,int i,int j)
{
int temp=nums[i];
nums[i]=nums[j];
nums[j]=temp;
for(int k=0;k<nums.length;k++)
{
nums[k]=nums[k];
}
return nums;
}
}

How can I invoke a method and print its result?

How can I invoke a method and print its result?
public class Test {
public static int main (String args[]){
System.out.println(total);
}
public int numbers (int a, int b){
int total;
total = a + b;
return = total;
}
}
Try this instead:
public class Test {
public static void main (String args[]){
System.out.println(numbers(1, 2));
}
public static int numbers(int a, int b){
int total;
total = a + b;
return total;
}
}
Variables are scoped to the method or class in which they are defined, therefore the 'total' variable is accessible only in the 'numbers' method
public class Test {
public static void main (String args[]){
System.out.println(numbers(anumber,bnumber));
}
public static int numbers (int a, int b){
int total;
total = a + b;
return total;
}

java inheritence on eclipse

I am learning inheritance and while doing so on Eclipse, I get an error when trying to run the following program:
import java.io.*;
import java.util.*;
public class singinh
{
void sub(int a, int b)
{
int c = a-b;
System.out.println("Diff is"+c);
}
}
public class singinh1 extends singinh {
int a,b;
void add(int a, int b)
{
this.a=a;
this.b=b;
System.out.println("Sum is"+a+b);
}
public static void main(String args[])
{
singinh1 s = new singinh1();
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
s. add(a,b);
s.sub(a,b);
}
}
The error that I get is "Error: Could not find or load main class superex$A"; What is causing this error, and how do I fix it?
As you start with java the best thing to do is to create 2 files singinh.java and singinh1.java, move the related code into the corresponding file and launch your java command using singinh1 as main class.
In singinh.java you will have:
public class singinh
{
void sub(int a, int b)
{
int c = a-b;
System.out.println("Diff is"+c);
}
}
In singinh1.java you will have:
import java.io.*;
import java.util.*;
public class singinh1 extends singinh {
int a,b;
void add(int a, int b)
{
this.a=a;
this.b=b;
System.out.println("Sum is"+a+b);
}
public static void main(String args[])
{
singinh1 s = new singinh1();
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
s. add(a,b);
s.sub(a,b);
}
}
Then you will be able to launch singinh1
In java you cannot have more than one public class in the same source file. Also the name of the source file should be the name of the public class in that source file exactly.
Since your main method is in "singinh1" class , keep it as the public class and remove public keyword from "singinh" class. Name the source file name to
singinh1.java .
Modified code :
import java.io.*;
import java.util.*;
class singinh
{
void sub(int a, int b)
{
int c = a-b;
System.out.println("Diff is"+c);
}
}
public class singinh1 extends singinh {
int a,b;
void add(int a, int b)
{
this.a=a;
this.b=b;
System.out.println("Sum is"+a+b);
}
public static void main(String args[])
{
singinh1 s = new singinh1();
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
s. add(a,b);
s.sub(a,b);
}
}

selection sort Algorithm java

I was debugging my code and found a problem with my Selection Sort Algorithm.
The code below almost sorts it, but I cant understand why all of it is not sorted.
have tried everything but to no avail.
import java.util.Random;
public class Help
{
//private static int[] myarray=new int[20];
private static int[] array;
public static void main(String[] args)
{
array=new int[20];
fillArrayRandom(array);
sortAscending(array);
///This is the bit that does not do what it is meant to do!
for(int i=0;i<array.length;i++)
{
// System.out.printf("Testing %d%n",myarray[i]);
}
}
public static void fillArrayRandom(int[] array)
{
int i;
for(i=0;i<array.length;i++)
{
array[i]=getRandomNum();
}
}
public static int getRandomNum()``
{
Random num=new Random();
int TestNumber=num.nextInt(2000);
return TestNumber;
}
public static void sortAscending(int[] array)
{
int smallest;
for(int i=0;i<array.length-1;i++)
{
smallest=i;
for(int index=i+1;index<array.length;index++)
{
if(array[index]<array[smallest])
smallest =index;
swap(i,smallest);
}
System.out.printf("%d%n",array[i]);
}
}
public static void swap(int first,int second)
{
int temporary=array[first];
array[first]=array[second];
array[second]=temporary;
}
}
You need to swap after the inner loop has completed:
public static void sortAscending(int[] array)
{
int smallest;
for(int i=0;i<array.length-1;i++)
{
smallest=i;
for(int index=i+1;index<array.length;index++)
{
if(array[index]<array[smallest])
smallest =index;
}
if (i != smallest) swap(i,smallest);
System.out.printf("%d%n",array[i]);
}
}

Categories

Resources