•
•
•
•
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
![]() |
•
•
Join Date: Mar 2005
Posts: 73
Reputation:
Rep Power: 4
Solved Threads: 0
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..
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..
•
•
Join Date: Jun 2004
Posts: 604
Reputation:
Rep Power: 6
Solved Threads: 6
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
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
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
![]() |
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
adsense adult advertising blogging bomb book business coding copyright development engine failure forum google internet java legal malware marketing mcafee microsoft microsoft sdk for java microsystems monetization msn news open operating pagerank platform privacy programming publishing revenue search security seo sex silverlight software spyware sun support system technical web webmaster wiki wikipedia yahoo
- Behaviour of Binary Search Program (C)
- Making a simple Vocabualry search program (C++)
- Help With Binary Search Program (C++)
Other Threads in the Java Forum
- Previous Thread: Blocking vs. Non-Blocking Direct Communication
- Next Thread: Randomly choosing method


Linear Mode