Why sort data? Data is easy to organize and some algorithms require data to be ordered. This chapter describes three simple sorting methods: Bubble, selection, and insertion algorithms. The basic operation is compare and exchange. Bubble Sorting: Listing 3.1 The bubbleSort. java Program
// BubbleSort. java
// Demonstrates bubble sort
// To run this program: C> java BubbleSortApp
//-------------------------------------------------------------
-
Class ArrayBub
{
Private double [] a; // ref to array
Private int nElems; // number of data items
//-------------------------------------------------------------
-
Public ArrayBub (int max) // constructor
{
A = new double [max]; // create the array
NElems = 0; // no items yet
}
//-------------------------------------------------------------
-
Public void insert (double value) // put element into array
{
A [nElems] = value; // insert it
NElems ++; // increment size
}
//-------------------------------------------------------------
-
Public void display () // displays array contents
{
-70-
For (int j = 0; j <nElems; j ++) // for each element,
System. out. print (a [j] + ""); // display it
System. out. println ("");
}
//-------------------------------------------------------------
-
Public void bubbleSort ()
{
Int out, in;
For (out = nElems-1; out> 1; out --) // outer loop
(Backward)
For (in = 0; in <out; in ++) // inner loop (forward)
If (a [in]> a [in + 1]) // out of order?
Swap (in, in + 1); // swap them
} // End bubbleSort ()
//-------------------------------------------------------------
-
Private void swap (int one, int two)
{
Double temp = a [one];
A [one] = a [two];
A [two] = temp;
}
//-------------------------------------------------------------
-
} // End class ArrayBub
//////////////////////////////////////// ////////////////////////
Class BubbleSortApp
{
Public static void main (String [] args)
{
Int maxSize = 100; // array size
ArrayBub arr; // reference to array
Arr = new ArrayBub (maxSize); // create the array
Arr. insert (77); // insert 10 items
Arr. insert (99 );
Arr. insert (44 );
Arr. insert (55 );
Arr. insert (22 );
Arr. insert (88 );
Arr. insert (11 );
Arr. insert (00 );
Arr. insert (66 );
Arr. insert (33 );
Arr. display (); // display items
Arr. bubbleSort (); // bubble sort them
Arr. display (); // display them again
} // End main ()
} // End class BubbleSortApp Bubble sorting efficiency: N * N: Two-layer nested loop, algorithm efficiency is N * N; select sorting: // selectSort. java
// Demonstrates selection sort
// To run this program: C> java SelectSortApp
//-------------------------------------------------------------
-
Class ArraySel
{
Private double [] a; // ref to array
Private int nElems; // number of data items
//-------------------------------------------------------------
-
Public ArraySel (int max) // constructor
{
A = new double [max]; // create the array
NElems = 0; // no items yet
}
//-------------------------------------------------------------
-
Public void insert (double value) // put element into array
{
A [nElems] = value; // insert it
NElems ++; // increment size
}
//-------------------------------------------------------------
-
Public void display () // displays array contents
{
For (int j = 0; j <nElems; j ++) // for each element,
System. out. print (a [j] + ""); // display it
System. out. println ("");
}
//-------------------------------------------------------------
-
Public void selectionSort ()
{
Int out, in, min;
For (out = 0; out <nElems-1; out ++) // outer loop
{
-76-
Min = out; // minimum
For (in = out + 1; in <nElems; in ++) // inner loop
If (a [in] <a [min]) // if min greater,
Min = in; // we have a new min
Swap (out, min); // swap them
} // End for (outer)
} // End selectionSort ()
//-------------------------------------------------------------
-
Private void swap (int one, int two)
{
Double temp = a [one];
A [one] = a [two];
A [two] = temp;
}
//-------------------------------------------------------------
-
} // End class ArraySel
//////////////////////////////////////// ////////////////////////
Class SelectSortApp
{
Public static void main (String [] args)
{
Int maxSize = 100; // array size
ArraySel arr; // reference to array
Arr = new ArraySel (maxSize); // create the array
Arr. insert (77); // insert 10 items
Arr. insert (99 );
Arr. insert (44 );
Arr. insert (55 );
Arr. insert (22 );
Arr. insert (88 );
Arr. insert (11 );
Arr. insert (00 );
Arr. insert (66 );
Arr. insert (33 );
Arr. display (); // display items
Arr. selectionSort (); // selection-sort them
Arr. display (); // display them again
} // End main ()
} // End class SelectSortApp
//-------------------------------------------------------------
Algorithm efficiency: N * N; insert sorting: basic and important sorting algorithms are partially ordered, and data on the left side of an identifier is ordered. Listing 3.3 The insertSort. java Program
// InsertSort. java
// Demonstrates insertion sort
-81-
// To run this program: C> java InsertSortApp
//-------------------------------------------------------------
-
Class ArrayIns
{
Private double [] a; // ref to array
Private int nElems; // number of data items
//-------------------------------------------------------------
-
Public ArrayIns (int max) // constructor
{
A = new double [max]; // create the array
NElems = 0; // no items yet
}
//-------------------------------------------------------------
-
Public void insert (double value) // put element into array
{
A [nElems] = value; // insert it
NElems ++; // increment size
}
//-------------------------------------------------------------
-
Public void display () // displays array contents
{
For (int j = 0; j <nElems; j ++) // for each element,
System. out. print (a [j] + ""); // display it
System. out. println ("");
}
//-------------------------------------------------------------
-
Public void insertionSort ()
{
Int in, out;
For (out = 1; out <nElems; out ++) // out is dividing line
{
Double temp = A [out]; // remove marked item
In = out; // start shifts at out
While (in> 0 & A [in-1]> = temp) // until one is
Smaller,
{
A [in] = A [in-1]; // shift item right,
-- In; // go left one position
}
A [in] = temp; // insert marked item
} // End
} // End insertionsort ()
//-------------------------------------------------------------
-
-82-
} // End class arrayins
//////////////////////////////////////// ////////////////////////
Class insertsortapp
{
Public static void main (string [] ARGs)
{
Int maxsize = 100; // array size
Arrayins arr; // reference to array
Arr = new ArrayIns (maxSize); // create the array
Arr. insert (77); // insert 10 items
Arr. insert (99 );
Arr. insert (44 );
Arr. insert (55 );
Arr. insert (22 );
Arr. insert (88 );
Arr. insert (11 );
Arr. insert (00 );
Arr. insert (66 );
Arr. insert (33 );
Arr. display (); // display items
Arr. insertionSort (); // insertion-sort them
Arr. display (); // display them again
} // End main ()
} // End class InsertSortApp algorithm efficiency: N * N object sorting: // objectSort. java
// Demonstrates sorting objects (uses insertion sort)
// To run this program: C> java ObjectSortApp
//////////////////////////////////////// ////////////////////////
Class Person
{
Private string lastname;
Private string firstname;
Private int age;
//----------------------------------------------------------
-
Public Person (string last, string first, int)
{// Constructor
Lastname = last;
Firstname = first;
Age =;
}
//----------------------------------------------------------
-
Public void displayperson ()
{
System. Out. Print ("Last Name:" + lastname );
System. Out. Print (", first name:" + firstname );
System. Out. println (", age:" + age );
}
//----------------------------------------------------------
-
Public String getlast () // get last name
{Return lastname ;}
} // End class Person
//////////////////////////////////////// ////////////////////////
Class ArrayInOb
{
Private Person [] a; // ref to array
Private int nElems; // number of data items
-85-
//-------------------------------------------------------------
-
Public ArrayInOb (int max) // constructor
{
A = new Person [max]; // create the array
NElems = 0; // no items yet
}
//-------------------------------------------------------------
-
// Put person into array
Public void insert (String last, String first, int age)
{
A [nElems] = new Person (last, first, age );
NElems ++; // increment size
}
//-------------------------------------------------------------
-
Public void display () // displays array contents
{
For (int j = 0; j <nElems; j ++) // for each element,
A [j]. displayPerson (); // display it
System. out. println ("");
}
//-------------------------------------------------------------
-
Public void insertionSort ()
{
Int in, out;
For (out = 1; out <nElems; out ++) // out is dividing line
{
Person temp = a [out]; // remove marked person
In = out; // start shifting at out
While (in> 0 & // until smaller one
Found,
A [in-1]. getLast (). compareTo (temp. getLast ()> 0)
{
A [in] = a [in-1]; // shift item to the right
-- In; // go left one position
}
A [in] = temp; // insert marked item
} // End
} // End insertionSort ()
//-------------------------------------------------------------
-
} // End class ArrayInOb
//////////////////////////////////////// ////////////////////////
-86-
Class ObjectSortApp
{
Public static void main (String [] args)
{
Int maxSize = 100; // array size
ArrayInOb arr; // reference to array
Arr = new ArrayInOb (maxSize); // create the array
Arr. insert ("Evans", "Patty", 24 );
Arr. insert ("Smith", "Doc", 59 );
Arr. insert ("Smith", "Lorraine", 37 );
Arr. insert ("Smith", "Paul", 37 );
Arr. insert ("Yee", "Tom", 43 );
Arr. insert ("Hashimoto", "Sato", 21 );
Arr. insert ("Stimson", "Henry", 29 );
Arr. insert ("Velasquez", "Jose", 72 );
Arr. insert ("Vang", "Minh", 22 );
Arr. insert ("creden"", "Lucinda", 18 );
System. out. println ("Before sorting :");
Arr. display (); // display items
Arr. insertionSort (); // insertion-sort them
System. out. println ("After sorting :");
Arr. display (); // display them again
} // End main ()
} // Comparison of simple sorting of end class ObjectSortApp: Bubble is suitable for sorting small data volumes. If you choose to sort more and less, it is suitable for the case where the amount of data is small and the exchange is more time-consuming than the compare data; insert sorting is the most effective of the three simple sorting statements.