0% found this document useful (0 votes)
9 views10 pages

Selection 1

Uploaded by

namyab2009
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)
9 views10 pages

Selection 1

Uploaded by

namyab2009
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/ 10

Selection

Sort
CONNECTING
Learning Intention
• Sorting is a process in which the elements of an
array are arranged in either ascending or descending
order.

• It is necessary to learn sorting techniques as it is


easier and faster to locate items in a sorted array.
Lesson Objective
• To understand Selection sort
• To analyze the working of Selection sort
• To create a code for sorting the elements in an array using
Selection Sort.
Success Criteria
At the end of this unit, you should be able to:

✓Sort Numerical, Character, Boolean and String Type of data in Ascending


order or Descending order using sorting techniques like Bubble sort and
Selection sort.

✓Predict the status of the array during or after the sorting process
INTRODUCTION
• Selection sort is a combination of searching
and sorting.

• The algorithm searches for the smallest element (in


case of ascending order) or the largest element (in case
of descending order) in the list and swaps it with the
value in the first position.

• Then the next smallest (or the next largest) value is


searched for in the remainder of the list and moved to
the second position in the array.

• This process is repeated till all the elements are in the


correct position. During each pass, the next smallest
(or largest) value is moved to its proper position in the
array.
Selection Sort Implementation

Selection sort is implemented through a Nested loop.


The inner loop finds the next smallest (or largest)
value and the outer loop places that value into its
proper location.

The number of times the sort passes through the array is one
less than the number of items in the array.

Remember, a "pass" is defined as one full trip through the


array which involves comparing and if necessary,
swapping elements.
How Selection Sort Works?

For the first position in the sorted list, the whole list is scanned sequentially. The first position
where 14 is stored presently, we search the whole list and find that 10 is the lowest value. So we
replace 14 with 10. After one iteration. 10, which happens to be the minimum value in the list,
appears in the first position of the sorted list.

For the second position, where 33 is residing, we start scanning the rest of the list in a
linear manner. We find that 14 is the second lowest value in the list and it should
appear at the second place. We swap these values.

After two iterations, two least values are positioned at the beginning in a sorted
manner.

The same process is applied to the rest of the items in the array.
Selection Sort Technique
Original unsorted array

Pass 1

Pass 2

Pass 3

Pass 4

Pass 5

Pass 6

Pass 7

Pass 8
Selection Sort code
class selection_sort {
public static void main( ) {
int [] a={4,2,3,7,1,6};
int min, temp;
for ( int i = 0; i <a.length-1; i++ ) {
min = i; //initialize min with i assuming it contains the smallest value
for(int j = i+1; j < a.length; j++) //locate smallest element between positions i+1 to a.length
{
if( a[j] < a[min] )
min = j;// element at index j has the smaller value. Hence store it in min
} //for loop
if(i!=min) {
temp = a[min]; //swap smallest found in position min with element in position i
a[min] = a[ i ];
a[ i ] = temp;
}
}
// displaying elements
for( int i=0;i<a.length;i++)
System.out.println(a[i]);
}
}
END

You might also like