User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 426,442 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,245 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 1123 | Replies: 1
Reply
Join Date: Mar 2005
Posts: 73
Reputation: nabil1983 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
nabil1983 nabil1983 is offline Offline
Junior Poster in Training

Help Search program help in modifications

  #1  
Mar 30th, 2005
Hi all,

This program below sorts arrays of integers.
It counts the number of times it compares pieces of
data to do the sort.With the array size set at 4 it always
takes 9 comparisons.

Does neone know how i can Modify it to read in a series of four 3-letter words and print them out in sorted
order. The program needs to keep a count of the number of string comparisons performed and never
take more than 6 comparisons to do the sort.
i was told that it can be done by altering the inner loop so that each
pass is shorter than the previous one. Problem is i cant seem to get it,
and it cant use an index array.


For example, if the user typed the following:
ant cat pan tap
The program should print out:
Initially: ant cat pan tap
Sorted: tap pan cat ant
Number of comparisons was 6

import javax.swing.*;
import java.util.*;
public class SearchPractice{
public static void main (String [] args)
{
final int SIZE = 4;
int numbers[] = new int[SIZE];
int totalWork =0; //keep count of operations done
fill_array (numbers, SIZE);
System.out.print("Initially: ");
print_array (numbers, SIZE);
totalWork = bubble_sort (numbers, SIZE);
System.out.print("Sorted: ");
print_array (numbers, SIZE);
System.out.println
("Number of Comparisons was " + totalWork);
System.exit(0);
}
// Sort an array of integers using (an inefficient) variant of
// bubblesort
public static int bubble_sort (int array[], int arraySize)
{
int workDone = 0;
for (int pass = 0; pass < arraySize - 1; pass++)
{
for(int counter = 0; counter < arraySize - 1; counter++)
{
workDone = workDone + 1;
if (array[counter] > array[counter+1])
swap (array, counter, counter+1);
}
}
return workDone;
}
// Exchange a given pair of values given by their positions
// in an array
public static void swap (int a [], int p1, int p2)
{
int temp;
temp = a[p1];
a[p1] = a[p2];
a[p2] = temp;
}
// Fill an array from the keyboard
public static void fill_array (int array[], int arraySize)
{
for(int counter = 0; counter < arraySize; counter++)
{
array[counter] = Integer.parseInt
(JOptionPane.showInputDialog (
"Number for position:"+ counter));
}
}
// Print the contents of an array to the screen
public static void print_array (int array [], int arraySize)
{
for(int counter= 0; counter < arraySize; counter++)
{
System.out.print(array[counter] + "\t");
}
System.out.println();
}
}


if neone knows neway to do this please let me know,,any help would be apreciated..
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2004
Posts: 604
Reputation: freesoft_2000 is an unknown quantity at this point 
Rep Power: 6
Solved Threads: 6
freesoft_2000 freesoft_2000 is offline Offline
Practically a Master Poster

Re: Search program help in modifications

  #2  
Apr 3rd, 2005
Hi everyone,

Throw your string datas into a string array and compare them in a loop. The number of comparisons is the final value of loop(if a break is required or not)

Richard West
Microsoft uses "One World, One Web, One Program" as a slogan.
Doesn’t that sound like "Ein Volk, Ein Reich, Ein Führer" to you, too?
— Eric S. Raymond

Tell me what type of software do you like and what would you pay for it

http://www.daniweb.com/techtalkforums/thread19660.html
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Java Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 2:49 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC