473,387 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

help on selection sort

mfshake
16
i am using only a driver and not a server. I know how to do selection sort for ints but can't figure out how to do it for Strings. I want to sort it my first letter only Here is my code that i started:

private static void selectionSort(String [] a)
{
String [] array = a;
int min, temp;

for (int index = 0; index < a.length - 1; index++)
{
min = index;
for (int scan = index + 1; scan < a.length; scan++)
if (a[scan].substring(0,1) < a[min].substring(0,1))
min = scan;

temp = a[min].substring(0,1);
a[min].substring(0,1) = a[index].substring(0,1);
a[index].substring(0,1) = temp;
}//close for
}//close selectionSort


help is appreciated
Apr 16 '08 #1
1 1470
sukatoa
539 512MB
i am using only a driver and not a server. I know how to do selection sort for ints but can't figure out how to do it for Strings. I want to sort it my first letter only Here is my code that i started:

private static void selectionSort(String [] a)
{
String [] array = a;
int min, temp;

for (int index = 0; index < a.length - 1; index++)
{
min = index;
for (int scan = index + 1; scan < a.length; scan++)
if (a[scan].substring(0,1) < a[min].substring(0,1))
min = scan;

temp = a[min].substring(0,1);
a[min].substring(0,1) = a[index].substring(0,1);
a[index].substring(0,1) = temp;
}//close for
}//close selectionSort


help is appreciated
You may read the Collator class.

there is a compare method there that has two parameters.
First string and second string. starts from left (when we read). just like we did on comparing numerical data types.

It returns negative if first string is less than second string.
returns zero if they are equal
and returns positive number if first string is greater than second string.

regards,
sukatoa
Apr 17 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: ritchie | last post by:
Hi, I am writing to ask if anyone can see why my array is not being sorted correctly? It's an array of 4 elements(ints 1,2,3,4) but after calling the selection sort it comes back sorted as...
8
by: hothead098 | last post by:
ASSIGNMENT (4) USING AND MANIPUPATING ARRAYS (Chapter 10 material) For this assignment you are to: 1) Create and manage arrays a) One of type integers (containing 10 elements). b) One of...
2
by: pixcee2000 | last post by:
1)a m*n is said to have a saddle point it some entrya is the smallest in row and largest in column j. prog that determine the saddle point if it exist? 2)write a prog to input an integer x?...
12
by: ShaveDave27 | last post by:
HI, I've created a Comparable interface class Person. Which has variables First_name, surname, month(Of birth), day(Of birth) , and birthday(which i have created from month and day). I have a...
0
by: =?Utf-8?B?TGV0emRvXzF0?= | last post by:
I'd like to create a Macro that will sort some raw data, apprx 20k lines, remove some lines based upon a condition in a certain column. Then copy this data into a new spreadsheet and sort the ...
1
by: The.Daryl.Lu | last post by:
Hi, two parts to my problem if someone can help address either one or both: 1. I want to SELECT everything in the table if it matches the criteria when the query button is pressed (this is just...
4
by: sialater | last post by:
Hello, I realise there are a lot of topics related to this problem but many of what I have found has run cold or unresolved. What I have is an addressbook clone where there are groups which have...
1
by: HarishAdea | last post by:
Hi, I am trying to run the JAVA pgm, but it is giving error as "selection does not contain a main type". The filename is "ScoreLeadSummary.java" when i try to run it or debug,it gives the pop...
5
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.