You are on page 1of 13

SriLanka Institute of Information Technology

Design and Analysis of Algorithms (214)


Assignment 1
Simulator for Selection Sort and Insertion Sort
Name: Pirinthan.G
DIT No: DIT/10/C2/0285

This Assignment is completely created by me.


-----------------Pirinthan.G
Selection Sort

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

After the Sorting

Insertion Sort
Before the Sorting

Sorting Process

After the Sorting

Above Diagrams are output of Simulator for selection and insertion sorts
(sample outputs of processing)

Source Code for Selection Sort.


public class selectionSort extends javax.swing.JFrame implements Runnable {
JLabel[] LabelArray;
Thread t;
int arr;
int[] numberArray;
// Creates new form selectionSort
public selectionSort() {
initComponents();
setLayout(null);
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String[] arr=textField1.getText().split(",");
LabelArray=new
JLabel[]
{lblshow1,lblshow2,lblshow3,lblshow4,lblshow5,lblshow6,lblshow7,lblshow8};
numberArray=new int[8];
for(int i=0;i<arr.length;i++)
{
try
{
numberArray[i]=Integer.parseInt(arr[i]);//convert to integer
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(rootPane, "Invalid Input Provided");
return;
}
}
for(int i=0;i<numberArray.length;i++)
{
LabelArray[i].setText(numberArray[i]+"");//assign to label sets
}
t=new Thread(this);
t.start(); //start thread
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new selectionSort().setVisible(true);}});}

public void run() {


//For Loop of the Selection Sort
for(int i=0; i<numberArray.length; i++){
try
//The current Index is selected As the minimum index
int index_of_min = i;
lbl.setVisible(true);
lblInfo.setText("Select as "+numberArray[index_of_min]+" The Minimum Element")
//The Nested for Loop
for (int j = i; j < numberArray.length; j++) {
//compare the values and find min value
if (numberArray[index_of_min] > numberArray[j]) {
index_of_min = j;
lbl.setText(numberArray[index_of_min]+"");
//change the locatios
lbl.setLocation(LabelArray[j].getLocation().x, LabelArray[j].getLocation().y + 20);
lblInfo.setText("Select as "+numberArray[index_of_min]+" The Minimum Element");
Thread.sleep(1000);
}
}
lblInfo.setText("Interchange "+numberArray[i]+" with "+numberArray[index_of_min]);
lbl.setVisible(false);
LabelArray[index_of_min].setText("");
LabelArray[i].setText("");
Thread.sleep(1000);
int z = numberArray[i];
numberArray[i] = numberArray[index_of_min];
LabelArray[i].setText(numberArray[index_of_min]+"");
numberArray[index_of_min] = z;
LabelArray[index_of_min].setText(numberArray[index_of_min]+"");
Thread.sleep(1000);
LabelArray[i].setForeground(Color.red);
} catch (InterruptedException ex) {
}
}
lblInfo.setText("Sort Complete");
String abc="";
for (int i= 0; i < 8; i++) {
abc=abc+numberArray[i]+" ";
}
JOptionPane.showMessageDialog(rootPane, "Sorted Array : "+abc);
}
}
// Variables declaration private javax.swing.JButton jButton1;

private javax.swing.JFrame jFrame1;


private javax.swing.JFrame jFrame2;
private javax.swing.JFrame jFrame3;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel lbl;
private javax.swing.JLabel lblInfo;
private javax.swing.JLabel lblshow1;
private javax.swing.JLabel lblshow2;
private javax.swing.JLabel lblshow3;
private javax.swing.JLabel lblshow4;
private javax.swing.JLabel lblshow5;
private javax.swing.JLabel lblshow6;
private javax.swing.JLabel lblshow7;
private javax.swing.JLabel lblshow8;
private java.awt.TextField textField1;
// End of variables declaration

Code for insertion Sort


import java.awt.Color;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
public class InsertionSort extends javax.swing.JFrame implements Runnable{
JLabel[] lblTemp;
JLabel[] lblShow;
Thread t;
int[] arr;
public InsertionSort() {
initComponents();
setLayout(null);

}
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();
}

public static void main(String args[]) {


java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new InsertionSort().setVisible(true);
}
});
}

public void run() {


try
//for Loop of the Insertion Sort
for (int i = 1; i < arr.length; i++) {
int j = i;
int key = arr[i];
lblinfo.setText("Select "+key+" as Key");
lblShow[i].setText("");
lblKey.setText(key+"");
//change the location to the lblkey
lblKey.setLocation(lblShow[i].getLocation().x,lblShow[i].getLocation().y+20);
Thread.sleep(100);
while ((j > 0) && (arr[j - 1] > key)) {
Thread.sleep(100);
arr[j] = arr[j - 1];
lblinfo.setText("Move "+arr[j-1]+" to arr["+j+"]");
Thread.sleep(1000);
lblKey.setLocation(lblShow[j-1].getLocation().x,lblShow[j-1].getLocation().y+20);
Thread.sleep(200);
lblShow[j - 1].setText("");
lblShow[j].setText(arr[j - 1]+"");
j--;
}
Thread.sleep(200);
arr[j] = key;
lblShow[j].setText(key+"");
lblKey.setText("");
Thread.sleep(500);
}
lblinfo.setText("Sort Complete");
} catch (InterruptedException ex) {
}
}
}

// Variables declaration - do not modify


private javax.swing.JButton btnSort;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel lblKey;
private javax.swing.JLabel lblShow1;
private javax.swing.JLabel lblShow2;
private javax.swing.JLabel lblShow3;
private javax.swing.JLabel lblShow4;
private javax.swing.JLabel lblShow5;
private javax.swing.JLabel lblShow6;
private javax.swing.JLabel lblShow7;
private javax.swing.JLabel lblShow8;
private javax.swing.JLabel lblinfo;
private javax.swing.JTextField txtInput;
// End of variables declaration
The above report created by me

You might also like