Ask Question, Ask an Expert


Ask Programming Language Expert


Sorting is a frequent array operation.  An array can be sorted if there is an ordering relation between the elements in array.  For illustration, if the array comprises a set of integer elements, we can order the elements according to integer number line.  A common technique whereby the elements in an array might be sorted is called the bubble sort.  Supposing an array of positive integers which we wish to sort according to the sequence represented by integer number line, the bubble sort operates as follows:

1.  Find out the two adjacent elements, X and Y, in the array such that X > Y and swap X and Y, then sort the resulting array.

2.  If there is no adjacent pair of the elements, X and Y, in the array such that X > Y, the list is sorted.

Note that the reason of swapping two elements X and Y that occur out of order is so that after the swap, the new list is closer to a sorted list.  After a sufficient amount of swapping, we must end up with all the elements in order.  For illustration given the array:

{1 2 5 4 7 3 6 8 10 9}

We would commence by the finding elements 5 and 4 and swap them to get:

{1 2 4 5 7 3 6 8 10 9}

We would then continue as follows:

{1 2 4 5 7 3 6 8 10 9}
{1 2 4 5 3 7 6 8 10 9}
{1 2 4 3 5 7 6 8 10 9}
{1 2 3 4 5 7 6 8 10 9}
{1 2 3 4 5 6 7 8 10 9}
{1 2 3 4 5 6 7 8 9 10}

The procedure is known as a bubble sort since elements slowly bubble up to their correct location.

Implement and Design a Java program which sorts a 10 element integer array using bubble sort procedure.  The elements of the array to be sorted must be supplied by the user (assume the user will not input duplicates).

Make a GUI front end for your bubble sort program.  The result must look something like that presented in Figure.  You might use any graphic element to make your display - JOptionPanesare the easiest and most basic tools accessible to you, but you might also experiment with additional GUI controls such as JFrames, JPanels, etc.  I would advise you to use objects from the Swing library (those objects begin with 'J') as opposed to objects from AWT library - Swing objects are a little easier to use and are ultimately more flexible and robust than AWT objects.

In the illustration below, there are 10 text fields on a JFrame background to allow input of array elements (remember that for each text field you should press the carriage return key to invoke the listener).  When the array has been populated, we press the start button, at which point the given array is output as a label.  We then sort the output and result the sorted array.

Hint: Don’t attempt to make the GUI too sophisticated.  Just concentrate on producing a working result (although sound programming techniques should still be applied).

1061_buble sort_1.jpg

Figure: Bubble sort GUI (partial ex)

Remember to prepare the source code for each class in a separate file which should have the same name as the class name together with extension .java.  Remember also that by convention, class names commence with a capital letter.

Programming Language, Programming

  • Category:- Programming Language
  • Reference No.:- M91529

Have any Question? 

Related Questions in Programming Language

Assignmentanalyze each problem develop a solution and

Assignment Analyze each problem, develop a solution and implement your solution. Copy and paste your program and a sample output below each problem. Define the class Point in a rectangular coordinate system. This class m ...

Question 1 batteriesi want to use a galvanic cell to power

Question 1: Batteries I want to use a galvanic cell to power a 60-watt light bulb. Complete the following steps to determine how long the galvanic cell will power the light bulb before running out. a.) The galvanic cell ...

Assignmenthomework assignments must be individuals original

Assignment Homework assignments must be individual's original work. Discussion without taking notes will be fine, but collaborations will not be tolerated. Dishonesty will result in an automatic F grade and a formal char ...

Assignment on stackswrite a program that evaluates

Assignment on Stacks Write a program that evaluates arithmetic expressions in infix notation that are not necessarily fully parenthesized. An arithmetic operation +, -, * or / has its usual precedence and associativity. ...

Assignmentwrite a program that calculates the intersection

Assignment Write a program that calculates the intersection of two sets of numbers. The sets can be represented using arrays. The general idea is that A!=0 if i is in the set and A==0 if it is not. Array element A can th ...

Create a class rectangle with attributes length and width

Create a class Rectangle with attributes length and width, each of which defaults to 1. Provide methods that calculate the rectangle's perimeter and area. It has set and get methods for both length and width. The set met ...

Resources appendices a amp dexamine the following

Resources: Appendices A & D Examine the following situation: Your company, Datamax, Inc., is in the process of automating its payroll systems. Your manager has asked you to create a program that calculates overtime pay f ...

Complete case programming assignment at the end of chapter

Complete Case Programming Assignment at the end of Chapter 4, page 283. Be sure to create an event planning document for each event in the program prior to writing code. Write the code that will execute the program requi ...

1 solve a simple payroll calculation find amount of pay

1. Solve a simple payroll calculation. Find amount of pay given hours worked and hourly rate. (pay = hourly rate * hours worked.) Run this values (hours = 30 and rate = 8.52) and with (hours = 53 and rate = 11.54). Show ...

Generate code for the following three-address statements

Generate code for the following three-address statements assuming stack allocation where register SP points to the top of the stack. a) x = 1 b) x = a c) x = a + 1 d) x = a + b e)The two statements x = b * c y = a + x

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Section onea in an atwood machine suppose two objects of

SECTION ONE (a) In an Atwood Machine, suppose two objects of unequal mass are hung vertically over a frictionless

Part 1you work in hr for a company that operates a factory

Part 1: You work in HR for a company that operates a factory manufacturing fiberglass. There are several hundred empl

Details on advanced accounting paperthis paper is intended

DETAILS ON ADVANCED ACCOUNTING PAPER This paper is intended for students to apply the theoretical knowledge around ac

Create a provider database and related reports and queries

Create a provider database and related reports and queries to capture contact information for potential PC component pro

Describe what you learned about the impact of economic

Describe what you learned about the impact of economic, social, and demographic trends affecting the US labor environmen