Professional Documents
Culture Documents
Selection sort performs sorting by repeatly putting the largest element in the unprocessed portion
of the array to the end of the this unprocessed portion until the whole array is sorted
It has O(n2) time complexity.
1.Find the minimum value in the list
2.Swap it with the value in the first position
3.Repeat the steps above for the remainder of the list.
Insertion Sort
Insertion sort is a simple sorting algorithm, a comparison sort in which the sorted array (or list) is
built one entry at a time. It is much less efficient on large lists than more advanced algorithms
such as quicksort, heapsort, or merge sort. .
1. Sort the first two elements
2. Then, take the third element and put it in the appropriate position relative to
the first two
3. Proceed iteratively until all of the elements are sorted
Insertion sort belongs to the O(n2) sorting algorithms
Interfaces of Simulators
Selection Sort
Befor the Sorting
Sorting Process
Insertion Sort
Before the Sorting
Sorting Process
Above Diagrams are output of Simulator for selection and insertion sorts
(sample outputs of processing)
}
private void btnSortActionPerformed(java.awt.event.ActionEvent evt) {
//get the values in the text field then split with ","
String[] arr=txtInput.getText().split(",");
//Assign the Jlables which are used to display the animation
lblShow=new JLabel[]
{lblShow1,lblShow2,lblShow3,lblShow4,lblShow5,lblShow6,lblShow7,lblShow8};
//If we enter more than 8 elements show message Please Enter only 8 Elements
if(arr.length>8)
{
JOptionPane.showMessageDialog(rootPane, "Please Enter only 8 Elements");
return;
}
//If we enter less than 2 elements show message Please Enter atleast 2 Elements
if(arr.length<2)
{
JOptionPane.showMessageDialog(rootPane, "Please Enter atleast 2 Elements");
return;
}
// difined array only 8 elements
this.arr=new int[8];
for(int i=0;i<arr.length;i++)
{
try
{
//convert to integer
this.arr[i]=Integer.parseInt(arr[i]);//convert to integer
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(rootPane, "Invalid Input Provided");
return;
}
}
for(int i=0;i<this.arr.length;i++){
//assign to label sets
lblShow[i].setText(this.arr[i]+"");
}
t=new Thread(this);
t.start();
}