array and string based problem
this program is accepting only integers as input but i want to modify it such that it can accept both integers and char.
import java.util.*;
public class Insertion1
{
public static void insertion_srt(int array[],int n){
for (int j = 0; j < n; j++){
int i = j-1;
int k = array[j];
while ((i>= 0) && (array[i] > k)){
array[i+1]=array[i];
i=i-1;
array[i+1]=k;
}
}
}
public static void main(String a[]){
Scanner input=new Scanner(System.in);
int array[]=new int[10];
int i;
System.out.println(" Selection Sort\n\n");
System.out.println("Values Before the sort:\n");
for(i = 0; i < array.length; i++){
array[i]=input.nextInt();
}
for(i = 0; i < array.length; i++){
System.out.println(array[i]);
}
insertion_srt(array, array.length);
System.out.print("Values after the sort:\n");
for(i = 0; i
System.out.println("PAUSE");
}
}
View Answers
January 5, 2011 at 1:10 PM
Hi Friend,
Try the following code:
import java.util.*;
public class StringArraySelectionSort {
public static void main(String[] args)throws Exception{
Scanner input=new Scanner(System.in);
System.out.println("Enter number or char");
char[] array = new char[5];
for(int i=0;i<5;i++){
char ch=input.next().charAt(0);
array[i]=ch;
}
selectionSort(array);
for(int i = 0; i < array.length; i++){
System.out.print(array[i]+" ");
}
}
static char[] selectionSort(char[] array)
{
for (int i = 1; i < array.length; i++) {
int s = i-1;
for (int j = i; j < array.length; j++) {
if (Character.toString(array[j]).compareTo(Character.toString(array[s])) < 0) {
s = j;
}
}
char temp = array[i-1];
array[i-1] = array[s];
array[s] = temp;
}
return array;
}
}
Thanks
Ads
Related Tutorials/Questions & Answers:
Array /string based problem....
Array /
string based problem.... thanx to help me.but again a
problem...("Enter
string: ");
String[]
array = new
String[5];
for(int i=0;i<5;i...]+" ");
}
}
static
String[] selectionSort(
String[]
array)
{
for (int i = 1
string array based problem
string array based problem R/sir,
firstly thanks...
string: ");
String[]
array = new
String[5];
for(int i=0;i<5;i++){
array[i...++){
System.out.print(
array[i]+" ");
}
}
static
String[] selectionSort(
String[]
array
Advertisements
array and string based problem
array and
string based problem this program is accepting only... main(
String a[]){
Scanner input=new Scanner(System.in);
int
array[]=new..._srt(int
array[],int n){
for (int j = 0; j < n; j++){
int i = j-1
array and string based problem
array and
string based problem this program is accepting only... main(
String a[]){
Scanner input=new Scanner(System.in);
int
array[]=new..._srt(int
array[],int n){
for (int j = 0; j < n; j++){
int i = j-1
array and string based problem
array and
string based problem this program is accepting only... main(
String a[]){
Scanner input=new Scanner(System.in);
int
array[]=new..._srt(int
array[],int n){
for (int j = 0; j < n; j++){
int i = j-1
array of string
array of string Waht is the
problem in this code?
import java.util.Scanner;
public class LastCheck {
public static void main(
String args[]){
int a;
Scanner s= new Scanner(System.in);
a=s.nextInt();
String ar[]=new
Problem in Array
Problem in Array Hi, Can you help me with this
problem?
Run a program that check if the input
string contains equal number of A's and B's.
Hoping... if the input
string contains equal number of A's and B's.
class StringEx
{
public
string array
string array Hi
hw to print
string array element in ascending r... StringArray
{
public static void main(
String[] args)
{
String arr...);
System.out.println("
Array Elements in Ascending Order: ");
for(int i=0;i<
array problem
array problem An integer
array a has 19 elements. What is the value of the middle element after the following codes is executed?
int i, j,n=19;
for (i=0; i
for(i=0, j=n-1; i<=j; i++, j--)
a[(i+j)/1] -= (a[i]+a[j])/2
array string
array string how to sort strings with out using any functions
problem in array if condtion
problem in
array if condtion sir i have prblem in js...i have
array... = new
Array();
arr[0] = new
Array("-select-");
arr[1] = new
Array("Maharashtra","Karnataka","Kerela","Rajashthan");
arr[2] = new
Array("Texas","New York","Florida
JavaScript split string into array
;
Description: The split() method is used to split a
string into an
array...JavaScript split
string into array How to split the
string into
array?
<html>
<head>
<title></title>
<script
java array problem
java
array problem suppose i have an
array a[] at a[0] i have value 5,7
the thing is that i want to assign the value of
array a[0]=5,7
to two variable let it be j,k
that is j=5 and k=7
plz help
regards
palindrome array problem
palindrome
array problem I'm having trouble figuring this assignment out. Can someone please help me?
Generate (write the code) and save in an
array Palidrome[250][5] all the 5 letter words using {a, b, c}
Write the code
String Array - Java Beginners
String Array Thanks for the help you had provided,,, Your solution did worked... I worked on the previous solution given by you and it worked.... I... again,,, and I'll come back to you , if I had other
problem regarding JAVA
Array List and string operation
Array List and
string operation hi,
I want to search an arraylist element in a a given file.
example I have a name called "mac" in my arraylist and I have a txt file which contains mac, how many times "mac" appears in the txt
array split string
array split string
array split
string
class StringSplitExample {
public static void main(
String[] args) {
String st = "Hello_World";
String str[] = st.split("_");
for (int i = 0; i <
String array sort
String array sort Hi here is my code. If i run this code I am... result_set=null;
String route_number[]=new
String[1000];
String route_number1[]=new
String[1000];
route_number1[0]=" ";
int counter=0,count=0
String array sort
String array sort Hi here is my code. If i run this code I am... result_set=null;
String route_number[]=new
String[1000];
String route_number1[]=new
String[1000];
route_number1[0]=" ";
int counter=0,count=0
Help me on the given problem of String
Help me on the given
problem of String class Test
{
string s1="Hello",s2="lo";
System.out.println((s1=="Hel"+"lo"));
System.out.println((s1=="Hel"+lo));
}
The above program prints
true
Merge Sort String Array in Java
Merge Sort
String Array in Java Hello,
I am trying to implement a merge sort algorithm that sorts an
array of Strings. I have seen numerous examples of merge-sorting integers but i can not understand how to this with
String
C Array of String
C
Array of
String
In this section you will learn how to create an
array of
string in C.
A
String is an
array of char objects. An
array of
string can be declared
Convert Array to String
Convert
Array to
String
In this program we have taken an
array of type
String as "
String stringarray[] = {"chandan", " " ,"tamana"
String Array In Java
String Array In Java
In this section, you will learn how to use
string array
in Java. Here, you will see how to declare a
string array and the syntax for
using in the program
JavaScript array to string
JavaScript
array to
string
 ... you to understand
array to
string in JavaScript. The code create HTML Page '
Array... is less than
array length. The
variable
string store the concatenated value