[go: up one dir, main page]

0% found this document useful (0 votes)
0 views8 pages

ARRAYS - Part 3 - Selection Sort

The document outlines the selection sort technique, which involves selecting the smallest element from an unsorted array and placing it in order until the entire array is sorted. It provides Java code examples for sorting both numbers and strings using this method. Additionally, it includes programming exercises related to sorting student marks and employee names using selection sort.

Uploaded by

amruthabs125
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views8 pages

ARRAYS - Part 3 - Selection Sort

The document outlines the selection sort technique, which involves selecting the smallest element from an unsorted array and placing it in order until the entire array is sorted. It provides Java code examples for sorting both numbers and strings using this method. Additionally, it includes programming exercises related to sorting student marks and employee names using selection sort.

Uploaded by

amruthabs125
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

ARRAYS

PART - 3
2

AGENDA 1. TECHNIQUE OF SORTING – SELECTION SORT


2. PROGRAMS
S SORTING IN JAVA 3

Sorting means to put data in order; either numerically or alphabetically.


There are various sorting techniques:
Merge sort, Insertion sort, Bubble sort, Selection sort and so on.
SELECTION SORT BASIC IDEA:
Select the smallest element from the unsorted array.
Put it in the first position of the unsorted part of the array. Again, select the next
smallest element and put it in in the first position of the current range. This process
will continue till the last but one element.
The array will soon have a sorted portion of the array and an unsorted portion of the
array till the entire array is sorted.
SELECTION SORT TECHNIQUE Values to be arranged in ascending order.
4

Unsorted array PASS 5

PASS 1 PASS 6

PASS 2 PASS 7

PASS 3
Sorted array

PASS 4
5
First Accept Numbers Into The Array.

for( int i =0; i < arr.length - 1; i++)


{
int min = i;
for( int j =i+1; j < arr.length; j++)
Selection Sort Program {
Arranging numbers in if(arr[j]<arr[min])
{ min = j; } // searching for lowest index
ascending order
}
int temp=arr[min];
arr[min]=arr[i];
arr[i]=temp;
}
Finally Display The Sorted Array.
6
First Accept Numbers Into The Array.

for( int i =0; i < arr.length - 1; i++)


{
int min = i;
Selection Sort Program for( int j =i+1; j < arr.length; j++)
{
Arranging Strings in if(arr[j].compareTo(arr[min])<0)
alphabetical order. { min = j; } // searching for lowest index
}
String temp=arr[min];
arr[min]=arr[i];
arr[i]=temp;
}
Finally Display The Sorted Array.
7

PROGRAMS
1. Initialize an array with the computer marks(out of 100) of 10 students, initialize another
array with the corresponding names of the Students. Under appropriate heading, using
Selection sort technique, display the marks of the students from the lowest mark to the
highest mark along with the corresponding names of the students.
2. An organization has 150 employees. Accept the names of the employees along with the
PAN number (alphanumeric values) into two separate arrays. Sort the names of the
employees in alphabetical order. Display the sorted names along with the corresponding
PAN numbers using Selection sort technique.
8

Thanks!

You might also like