Beruflich Dokumente
Kultur Dokumente
[ December 2019 ]
1
Candidate’s Declaration
I hereby declare that the work, which is being presented in the minor project,
entitled “Sorting Algorithms “in partial fulfilment for the award of Degree of
“Bachelor of Technology” in Department of Computer Science & Engineering
with Specialization in Computer Engineering and submitted to the Department
of Computer Science & Engineering, Arya Institute of Engineering &
Technology, is a record of my own investigations carried under the Guidance of
Mr. Indrajeet sinha, Associate Professor, Department of Computer Science &
Engineering.
(Signature of Candidate)
2
Acknowledgment
We wish to express our deep sense of gratitude to our project guide Er. Manish Choubisa
Assistant Professor, Arya Institute of engineering and technology, Jaipur, for guiding us from
the inception till the completion of the project. We sincerely acknowledgment him for giving
his valuable guidance, support for literature survey, critical reviews and comments for our
project.
We would like to first of all express our thanks to Dr. Arvind agarwal, Chairman, Arya group
of colleges and Dr. Puja Agarwal, managing director, Arya group of colleges, for providing us
such a great infrastructure and environment for our overall development.
We express sincere thanks to Mr. Yogesh Bhomia, Principal, AIET for his kind cooperation and
extendible support toward the completion of our project Words are inadequate in offering our
thanks to Mr. SS Shekhawat, Head, Department of CSE, for consistent encouragement and
support for shaping our project in the presentable form.
We also like to express our thanks to all supporting CSE faculty members who have been a
constant source of encouragement for successful completion of the project.
3
ABSTRACT
It is a desktop application using core java that represent several sorting algorithms, such as
Merge sort, Quick sort, Insertion sort, Bubble sort, Selection sort and heap sort. The real-life
use for this project is to sort a set of given number by a number of sorting algorithms because
every sorting algorithm has it’s pro and cons.
Sorting algorithm is an algorithm that puts elements of a list in a certain order. The most
frequently used orders are numerical order and lexicographical order. Efficient sorting is
important for optimizing the efficiency of other algorithms (such as search and merge
algorithms) that require input data to be in sorted lists. Sorting is also often useful for canonical
zing data and for producing human-readable output.
4
ARYA INSTITUTE OF ENGINEERING & TECHNOLOGY
SP-40, RIICO Industrial Area, Jaipur (Raj)-302022
Certificate
This is to certify that the work, which is being presented in the Project stage-1
report for “SFC The Game master” submitted by Mr. Sachin Jha, Vishal, Vishal
Yadav, Ankit Kumar, students of fourth year (VII Sem) B.Tech. in Computer
Engineering as a partial fulfilment for the award of degree of bachelor of
technology is a record of student’s work carried out and found satisfactory for
submission.
5
Table of content
3. Certificate…………………………………………………………
4. Acknowledgement…………………………………………………
5. Abstract…………………………………………………………….
6. Table of content…………………………………………………….
7. List of Figure……………………………………………………….
8. Chapter 1 Introduction 8
9. Chapter 2 Requirements 10
a) 2.1 hardware
b) 2.2 Software
Conclusion 39
References 40
6
LIST OF FIGURES
Fig 3. Menu
Fig 5. Symptoms
Fig 8. Training
7
Chapter 1
Introduction
Sorting algorithm
A sorting algorithm is an algorithm that puts elements of a list in a certain order. The most
frequently used orders are numerical order and lexicographical order. Efficient sorting is
important for optimizing the efficiency of other algorithms (such
as search and merge algorithms) that require input data to be in sorted lists. Sorting is also
often useful for canonicalizing data and for producing human-readable output. More formally,
the output of any sorting algorithm must satisfy two conditions.
8
Merge sort: Divide the list of elements in two parts, sort the two parts individually and then
merge it.
TYPES OF SORTING
The techniques of sorting can be divided into two categories. These are:
Internal Sorting
External Sorting
Internal Sorting: If all the data that is to be sorted can be adjusted at a time in the main memory,
the internal sorting method is being performed.
External Sorting: When the data that is to be sorted cannot be accommodated in the memory at
the same time and some has to be kept in auxiliary memory such as hard disk, floppy disk,
magnetic tapes etc, then external sorting methods are performed.
The complexity of sorting algorithm calculates the running time of a function in which 'n'
number of items are to be sorted. The choice for which sorting method is suitable for a problem
depends on several dependency configurations for different problems. The most noteworthy of
these considerations are:
The length of time spent by the programmer in programming a specific sorting program
To get the amount of time required to sort an array of 'n' elements by a particular method, the
normal approach is to analyse the method to find the number of comparisons (or exchanges)
required by it. Most of the sorting techniques are data sensitive, and so the metrics for them
depends on the order in which they appear in an input array.
9
Various sorting techniques are analysed in various cases and named these cases as follows:
Best case
Worst case
Average case
Hence, the result of these cases is often a formula giving the average time required for a
particular sort of size 'n.' Most of the sort methods have time requirements that range from
O(nlog n) to O(n2).
10
Best Case Analysis (Bogus)
In the best case analysis, we calculate lower bound on running time of an algorithm. We must
know the case that causes minimum number of operations to be executed. In the linear search
problem, the best case occurs when x is present at the first location. The number of operations
in the best case is constant (not dependent on n). So time complexity in the best case would be
Θ(1).
1.BUBBLE SORT
Bubble sort is a simple sorting algorithm. This sorting algorithm is comparison-based algorithm
in which each pair of adjacent elements is compared and the elements are swapped if they are
not in order. This algorithm is not suitable for large data sets as its average and worst-case
complexity are of Ο(n2) where n is the number of items.
Bubble Sort compares each pair of array element unless the whole array is completely sorted
in an ascending order. This may cause a few complexity issues like what if the array needs no
more swapping as all the elements are already ascending.
To ease-out the issue, we use one flag variable swapped which will help us see if any swap
has happened or not. If no swap has occurred, i.e. the array requires no more processing to be
sorted, it will come out of the loop.
swapped = false
11
if list[j] > list[j+1] then
/* swap them */
swapped = true
end if
end for
break
end if
end for
2.INSERTION SORT
This is an in-place comparison-based sorting algorithm. Here, a sub-list is maintained which is
always sorted. For example, the lower part of an array is maintained to be sorted. An element
which is to be 'inserted in this sorted sub-list, has to find its appropriate place and then it has to
be inserted there. Hence the name, insertion sort.
The array is searched sequentially and unsorted items are moved and inserted into the sorted
sub-list (in the same array). This algorithm is not suitable for large data sets as its average and
worst-case complexity are of Ο(n2), where n is the number of items.
Algorithm
Now we have a bigger picture of how this sorting technique works, so we can derive simple
steps by which we can achieve insertion sort.
Pseudocode:
procedure insertionSort( A : array of items )
int holePosition
int valueToInsert
valueToInsert = A[i]
holePosition = i
A[holePosition] = A[holePosition-1]
holePosition = holePosition -1
end while
A[holePosition] = valueToInsert
end for
end procedure
13
3.Heap sort algorithm
Heap Sort is a popular and efficient sorting algorithm in computer programming. Learning how
to write the heap sort algorithm requires knowledge of two types of data structures - arrays and
trees.
The initial set of numbers that we want to sort is stored in an array e.g. [10, 3, 76, 34, 23, 32] and
after sorting, we get a sorted array [3,10,23,32,34,76]
Heap sort works by visualizing the elements of the array as a special kind of complete binary
tree called heap.
Implementation:
int heap_size = N;
14
build_maxheap(Arr);
heap_size = heap_size - 1;
max_heapify(Arr, 1, heap_size);
Complexity:
maxheap has complexity O(logN), build maxheap has complexity O(N) and we run
maxheap N−1 times in heap_sort function, therefore complexity of heapsort function
is O(NlogN).
4.SELECTION SORT
Selection sort is a simple sorting algorithm. This sorting algorithm is an in-place comparison-
based algorithm in which the list is divided into two parts, the sorted part at the left end and
the unsorted part at the right end. Initially, the sorted part is empty and the unsorted part is the
entire list.
The smallest element is selected from the unsorted array and swapped with the leftmost
element, and that element becomes a part of the sorted array. This process continues moving
unsorted array boundary by one element to the right.
This algorithm is not suitable for large data sets as its average and worst case complexities are
of Ο(n2), where n is the number of items.
15
Algorithm
Pseudocode
procedure selection sort
n : size of list
for i = 1 to n - 1
min = i
for j = i+1 to n
min = j;
end if
end for
if indexMin != i then
16
end if
end for
end procedure
Quick Sort is one of the most efficient sorting algorithms and is based on the splitting of an
array into smaller ones. The name comes from the fact that, quick sort is capable of sorting a
list of data elements significantly faster than any of the common sorting algorithms. And like
Merge sort, Quick sort also falls into the category of divide and conquer approach of problem-
solving methodology.
17
Pseudo algorithm for quick sort
at right place */
18
Chapter 2
REQUIREMENTS
2.1 Hardware requirements
There is no need for any special hardware requirements for this desktop application or SFC
(save from cancer) The-game-Master. A personal computer is capable enough to run this
desktop.
Java was originally developed by James Gosling at Sun Microsystems (Which has since been
acquired by oracle) and released in 1995 as a core component of Sun Microsystems' java
platform. The original and reference implementations Java compilers, virtual machines,
and class libraries were originally released by Sun under propriety licenses. As of May 2007, in
compliance with the specifications of the Java community process, Sun had relicensed most of
its Java technologies under the GNU general public license. Meanwhile, others have developed
alternative implementations of these Sun technologies, such as the GNU compiler for
JAVA (bytecode compiler), GNU Classpath (standard libraries), and IcedTea Web (browser
plugin for applets).
19
Java is a widely used programming language expressly designed for use in the distributed
environment of the internet. It is the most popular programming language for Android
smartphone applications and is also among the most favoured for the development of edge
devices and the internet of things.
Java is a programming language that produces software for multiple platforms. When a
programmer writes a Java application, the compiled code (known as bytecode) runs on most
operating systems (OS), including Windows, Linux and Mac OS. Java derives much of its
syntax from the C and C++ programming languages.
Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.)
It is one of the most popular programming languages in the world
It is easy to learn and simple to use
It is open-source and free
It is secure, fast and powerful
It has a huge community support (tens of millions of developers)
Java is an object-oriented language which gives a clear structure to programs and allows
code to be reused, lowering development costs
As Java is close to C++ and C#, it makes it easy for programmers to switch to Java or
vice versa
20
Java Swing
Java Swing is a part of Java Foundation Classes (JFC) that is used to create window-based
applications. It is built on the top of AWT (Abstract Windowing Toolkit) API and entirely
written in java.
Swing is a GUI widget toolkit for Java.[1] It is part of Oracle's Java Foundation Classes (JFC)
– an API for providing a graphical user interface (GUI) for Java programs.
Swing was developed to provide a more sophisticated set of GUI components than the earlier
Abstract Window Toolkit (AWT). Swing provides a look and feel that emulates the look and
feel of several platforms, and also supports a pluggable look and feel that allows applications to
have a look and feel unrelated to the underlying platform. It has more powerful and flexible
components than AWT. In addition to familiar components such as buttons, check boxes and
labels, Swing provides several advanced components such as tabbed panel, scroll panes, trees,
tables, and lists.[2]
Unlike AWT components, Swing components are not implemented by platform-specific code.
Instead, they are written entirely in Java and therefore are platform-independent. The term
"lightweight" is used to describe such an element.[3]
In December 2008, Sun Microsystems (Oracle's predecessor) released the CSS / FXML based
framework that it intended to be the successor to Swing, called JavaFX.[4]
The javax.swing package provides classes for java swing API such as JButton, JTextField,
JTextArea, JRadioButton, JCheckbox, JMenu, JColorChooser etc.
21
Most GUI class libraries in C++ are platform specific
22
Java Abstract Window Toolkit (AWT)
AWT contains large number of classes and methods that allows you to create and manage
graphical user interface (GUI) applications, such as windows, buttons, scroll bars,etc. The AWT
was designed to provide a common set of tools for GUI design that could work on a variety of
platforms. The tools provided by the AWT are implemented using each platform's native GUI
toolkit, hence preserving the look and feel of each platform. This is an advantage of using
AWT.But the disadvantage of such an approach is that GUI designed on one platform may look
different when displayed on another platform.
AWT is the foundation upon which Swing is made i.e Swing is a set of GUI interfaces that
extends the AWT. But now a days AWT is merely used because most GUI Java programs are
implemented using Swing because of its rich implementation of GUI controls and light-
weighted nature.
23
Component class:
Component class is at the top of AWT hierarchy. Component is an abstract class that
encapsulates all the attributes of visual component. A component object is responsible for
remembering the current foreground and background colors and the currently selected text font.
Container: Container is
a component in AWT that contains another component like button, text field, tables
etc. Container is a subclass of component class. Container class keeps track of components
that are added to another component.
Panel:
Panel class is a concrete subclass of Container. Panel does not contain title bar, menu
bar or border. It is container that is used for holding components.
Window class:
Window class creates a top level window. Window does not have borders and menu
bar.
Frame:
Frame is a subclass of Window and have resizing canvas. It is a container that contain
several different components like button, title bar, textfield, label etc. In Java, most of the AWT
applications are created using Frame window. Frame class has two different constructors,
24
CHAPTER 3
CODING
Frame.java:
package algo_Calc;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
/**
*/
EventQueue.invokeLater(new Runnable() {
try {
window.frame.setVisible(true);
25
} catch (Exception e) {
e.printStackTrace();
});
/**
*/
public Frame() {
initialize();
/**
*/
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
btnNewButton.addActionListener(new ActionListener() {
26
public void actionPerformed(ActionEvent e) {
obj.vis(1,"BUBBLE SORT");
frame.setVisible(false);
});
frame.getContentPane().add(btnNewButton);
btnNewButton_1.addActionListener(new ActionListener() {
obj.vis(4,"INSERTION SORT");
frame.setVisible(false);
});
frame.getContentPane().add(btnNewButton_1);
btnNewButton_2.addActionListener(new ActionListener() {
obj.vis(2,"SELECTION SORT");
frame.setVisible(false);
});
27
frame.getContentPane().add(btnNewButton_2);
btnNewButton_3.addActionListener(new ActionListener() {
obj.vis(5,"MERGE SORT");
frame.setVisible(false);
});
frame.getContentPane().add(btnNewButton_3);
btnNewButton_4.addActionListener(new ActionListener() {
obj.vis(3,"QUICK SORT");
frame.setVisible(false);
});
frame.getContentPane().add(btnNewButton_4);
btnNewButton_5.addActionListener(new ActionListener() {
obj.vis(6,"HEAP SORT");
28
frame.setVisible(false);
});
frame.getContentPane().add(btnNewButton_5);
Sortings.java
package algo_Calc;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextArea;
import java.awt.Font;
import java.awt.Color;
//import java.awt.Desktop;
29
//import java.io.File;
//import java.io.IOException;
class quiks
if(partition-1>start)
if(partition+1<end)
if(num[i]<pivot){
num[start]=num[i];
num[i]=temp;
30
start++;
num[start] = pivot;
num[end] = temp;
return start;
class merg{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
31
for (i = 0; i < n1; i++)
num[k] = L[i];
i++;
else
num[k] = R[j];
j++;
k++;
32
num[k] = L[i];
i++;
k++;
num[k] = R[j];
j++;
k++;
if (l < r)
int m = l+(r-l)/2;
mergeSort(num, l, m);
merge(num, l, m, r);
33
class heaps{
int largest = i;
largest = left;
largest = right;
if (largest != i)
int temp;
temp = num[i];
num[i]= num[largest];
num[largest] = temp;
34
void heapSort(int num[], int size)
int i;
int temp;
temp = num[0];
num[0]= num[i];
num[i] = temp;
heapify(num, i, 0);
int key;
/**
35
*/
EventQueue.invokeLater(new Runnable() {
try {
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
});
/**
*/
public Sortingss() {
initialize();
key=k;
tname="Hello";
tname="pop";
tname=str;
frame.setVisible(true);
36
return 0;
/**
*/
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
textArea_1.setBackground(Color.DARK_GRAY);
textArea_1.setForeground(Color.WHITE);
frame.getContentPane().add(textArea_1);
frame.getContentPane().add(textField);
37
textField.setColumns(10);
frame.getContentPane().add(lblEnterInput);
textArea.setLineWrap(true);
textArea.setEditable(false);
frame.getContentPane().add(textArea);
btnSubmit.addActionListener(new ActionListener() {
str = textField.getText();
for(int i=0;i<tok.length;i++){
num[i]=Integer.parseInt(tok[i]);
38
if(key==1)
int j;
while ( flag )
temp = num[ j ];
//swap elements
if(key==2){
39
int n = num.length;
int min_idx = i;
min_idx = j;
// element
num[min_idx] = num[i];
num[i] = temp;
tname="SELECTION SORT";
if(key==3){
int size=num.length;
40
obj2.quickSort(num,0,size-1);
tname="QUICK SORT";
if(key==4){
int i, ke, j;
ke = num[i];
j = i - 1;
num[j + 1] = num[j];
j = j - 1;
num[j + 1] = ke;
tname="INSERTION SORT";
if(key==5){
41
merg obj3=new merg();
obj3.mergeSort(num, 0, size);
tname="MERGE SORT";
if(key==6){
int size;
size=tok.length;
obj4.heapSort(num,size);
tname="HEAP SORT";
for(int i=0;i<tok.length;i++){
str1[i]=Integer.toString(num[i]);
fin=fin+" "+str1[i];
textArea.setText(fin);
textArea_1.setText(tname);
});
42
frame.getContentPane().add(btnSubmit);
frame.getContentPane().add(lblIntegerMustBe);
frame.getContentPane().add(lblYourOutput);
btnExit.addActionListener(new ActionListener() {
frame.setVisible(false);
});
frame.getContentPane().add(btnExit);
btnOpenCode.addActionListener(new ActionListener() {
if(key==1)
43
try{
pb.start();
}catch(Exception ew){
System.out.println(ew.toString());
//C:\\Users\\HARSH\\workspace\\Selection.txt
if(key==2){
try{
pb.start();
}catch(Exception ew){
System.out.println(ew.toString());
if(key==3){
try{
pb.start();
}catch(Exception ew){
System.out.println(ew.toString());
44
}
if(key==4){
try{
pb.start();
}catch(Exception ew){
System.out.println(ew.toString());
if(key==5){
try{
pb.start();
}catch(Exception ew){
System.out.println(ew.toString());
if(key==6){
try{
45
pb.start();
}catch(Exception ew){
System.out.println(ew.toString());
});
frame.getContentPane().add(btnOpenCode;}}
46
Chapter 4
Project flow
Step 1:
Step 2:
47
Step 3:
Step 4:
48
Step 5
49
Step 7:
Step 8:
50
Chapter 5.
Conclusion
51
Chapter 6
Reference
1. https://www.studytonight.com/java/java-awt.php
2. https://www.geeksforgeeks.org/analysis-of-algorithms-set-2-asymptotic-analysis/
3. https://www.w3schools.in/data-structures-tutorial/sorting-techniques/
4. https://en.wikipedia.org/wiki/Sorting
5. https://en.wikipedia.org/wiki/Sorting_algorithm#Popular_sorting_algorithms
52