This document discusses different types of sorting algorithms. It describes internal sorting, which sorts data within main memory, and external sorting, which sorts data that exceeds main memory size. It then explains three sorting algorithms - bubble sort, selection sort, and insertion sort. For each algorithm, it provides the basic steps/approach and includes Java code examples.
Sorting is the process of organizing data. Types include Internal Sorting (data in main memory) and External Sorting (large data requiring external memory).
Introduction to sorting methods including Bubble Sort, which sorts data by comparing adjacent elements through multiple passes until sorted.
A Java implementation of Bubble Sort is provided, demonstrating the sorting of an unsorted array via a stepwise approach.
Selection Sort is explained, focusing on repeatedly selecting the smallest element and swapping it to the correct position in the list.
Java code is provided for the Selection Sort algorithm, showcasing how to sort an array of numbers in ascending order.
Insertion Sort is effective for small numbers, organizing elements by comparing and inserting them into their correct index in multiple passes.
Conclusion and appreciation for the audience from the presenter.
SORTING
Sorting refers tooperations of
arranging a set of data in a given
order.
10 20 30 40 50 60
30 10 60 20 50 40 Unsorted List
Sorted List
3.
BASIC TYPES :
InternalSorting:
If all the data to be sorted can be
adjusted in main memory then it is
called as Internal Sorting.
External Sorting:
If data to be stored is large and aquires
external memory then the type is called
as External Sorting.
ALGORITHM
Bubble Sort:
Algorithm ofbubble sort includes two steps
repeated until the list is sorted.
Compare adjacent elements, if the
element on right side is smaller then
swap their positions.
Compare first element, second element
and so on on completion of Pass 1 the
largest element is at last position.
To sort numbersin ascending order using bubble sort
technique
import java.util.*;
public class bubbleSort{
public static void main(String a[]) throws Exception
{
int i,j,n;
int a[] = {50,10,30,20,40};
System.out.println("Values Before the sort:n");
for(i = 0; i < a.length; i++)
System.out.print( a[i]+" ");
System.out.println();
n=a.length;
12.
for(i = 1;i < n; i++)
{
for(j = 0; j < (n-i); j++)
{
if(a[j] > a[j+1]){
t = a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
System.out.print("Values after the sort:n");
for(i = 0; i <a.length; i++)
System.out.print(a[i]+" ");
} }
ALGORITHM
Selection Sort:
Here inselection sort the algorithm depends on the
zeroth element majorly.
The Zeroth element is compared with the first
element and if the element at right is found smaller
then their positions are swapped or exchanged.
The same procedure is carried with all elements of
the list resulting into a fully sorted list.
To sort numbersin ascending order using selection
sort technique
import java.util.*;
public class selectionSort{
public static void main(String a[]) throws Exception
{
int i,j;
int a[] = {23,15,29,11,1};
System.out.println("Values Before the sort:n");
for(i = 0; i < a.length; i++)
System.out.print( a[i]+" ");
System.out.println();
n=a.length;
20.
for(i = 0;i < n; i++)
{
for(j = i+1; j < n; j++)
{
if(a[i] > a[j]){
t = a[j];
a[j]=a[i];
a[i]=t;
}
}
}
System.out.print("Values after the sort:n");
for(i = 0; i <a.length; i++)
System.out.print(a[i]+" ");
}}
Mathematical applications :in the search for
greater value, or the smallest value. In
many other applications.
This method is effective when dealing
with small numbers .
23.
ALGORITHM
Insertion Sort
In insertionsort the elements are compared
and inserted to respective index place.
It starts with comparision of 1st and 0th
element in pass 1.
In pass 2 the second element is compared
with the 1st and 0th element.
Doing so with all the elements in the list
appropriate element is inserted by shifting
elements on right.
24.
public insertionSort( int[]a r r )
{
f or ( i n t i = 1; i < arr. Length; ++i)
{
i n t temp = a r r [ i ] ;
i n t pos = i ;
}
arr[ pos] = temp;
}
}
Select
while (arr[pos-1].CompareTo(temp) > 0 &&pos > 0)
{
Comparing
arr[ pos] = arr[pos-1];
pos--;
Shift
Insert
25.
75 57 2519 4
Pass 1: Number Of Passes = Max – 1 = 5 – 1 = 4
57 75 25 19 4
75 57 25 19 4 0,1