0% found this document useful (0 votes)
22 views22 pages

Learning Module 4 Cpen 65

Uploaded by

joshua.gobrin
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)
22 views22 pages

Learning Module 4 Cpen 65

Uploaded by

joshua.gobrin
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

Department of Industrial and

Information Technology

CPEN 65: Data Structures and


Algorithm Analysis

Simple Sorting

Prepared by:
RICARTE JAY P. RAVINA
Department of Industrial and
Information Technology

OBJECTIVE
Students will be able
to:

• Differentiate simple sorting algorithms in Java.


• Understand the concept of bubble sorting algorithm.
• Understand the concept of selection sorting algorithm.
• Understand the concept of insertion sorting algorithm.
Department of Industrial and
Information Technology

SORTING
Sorting is the process of rearranging the elements
of an array or list in a specific order, typically in
ascending or descending order. This is achieved using
a comparison operator to determine the new order of
elements. Sorting is a fundamental operation in
computer science and is used in various applications
such as searching, data management, and database
optimization.
Department of Industrial and
Information Technology

SORTING
As soon as you create a significant database, you will probably think of reasons
to sort it in various ways. Sorting data may also be a preliminary step to searching it.

All sorting techniques in this chapter are worth to examine but comparatively
slow and unsophisticated.

Besides being easier to understand, they are actually better in some


circumstances than the more sophisticated algorithms.
Department of Industrial and
Information Technology

TWO (2) STEPS OF SORTING


The three algorithms in this topic all involve two steps, executed over and over until the
data is sorted:

1. Compare two items.

2. Swap two items or copy one item.


Department of Industrial and
Information Technology

Example:

Unordered Group Ordered Group


Department of Industrial and
Information Technology

THREE (3) SIMPLE ALGORITHMS


➢ Bubble Sort

➢ Selection Sort

➢ Insertion Sort
Department of Industrial and
Information Technology

BUBBLE SORT
The bubble sort is known to be notoriously slow, but it is conceptually the simplest of the
sorting algorithms. And for that reason, is a good beginning for our exploration of sorting
techniques.

Rule of bubble sorting:


1. Compare two students.
2. If the one on the left is taller, swap them.
3. Move one position to the right.
Department of Industrial and
Information Technology

BUBBLE SORT
Upon the conclusion of the initial pass,
the highest individual shall be positioned
at the far right of the line.
Department of Industrial and
Information Technology

BUBBLE SORT
Visual representation of bubble sort:
Department of Industrial and
Information Technology

SAMPLE CODE: BUBBLE SORT


Department of Industrial and
Information Technology

SELECTION SORT
Selection Sort is a simple sorting
algorithm that works by repeatedly
selecting the minimum element from the
unsorted part of the array and placing it
at the beginning.
Department of Industrial and
Information Technology

SELECTION SORT
In the selection sort, you can no
longer compare only Students standing
next to each other. Thus, you'll need to
remember a certain Student’s height.
Making a pass through all the Students
and picking (or selecting, hence the name
of the sort) the shortest one.
Department of Industrial and
Information Technology

SELECTION SORT
It repeatedly finds the minimum
element in the unsorted part of the
array and swaps it with the element
at the current index. This process
continues until the entire array is
sorted.
Department of Industrial and
Information Technology

SELECTION SORT
Visual representation of selection sort:
Department of Industrial and
Information Technology

SAMPLE CODE: SELECTION SORT


Department of Industrial and
Information Technology

INSERTION SORT
Insertion Sort is a simple and intuitive
sorting algorithm that works similarly to
how we sort playing cards in our hands. It
is particularly efficient for small datasets
but can be less efficient for larger datasets
due to its quadratic time complexity.
Department of Industrial and
Information Technology

INSERTION SORT
Insertion Sort works by repeatedly taking

an element from the unsorted part of the

array and inserting it into its correct

position in the sorted part of the array.


Department of Industrial and
Information Technology

INSERTION SORT
How insertion sort algorithm works:
1. The first element in the array is assumed to be sorted. Take the second element and store it
separately in TEMP.

2. Compare the second element with the first element. If the first element is greater than stored
TEMP, then TEMP should be swapped with first element.

3. Move to the third element. Compare it with the elements to its left in the sorted portion of the
array. Elements will continue to shift to the right if it is greater than TEMP. If TEMP is greater
than the elements to the left, it will be sent back into the array.

4. Continue this process for each element in the array, one at a time, until the entire array is
sorted.
Department of Industrial and
Information Technology

INSERTION SORT
Visual representation of selection sort:
Department of Industrial and
Information Technology

SAMPLE CODE: INSERTION SORT


Department of Industrial and
Information Technology

THANK YOU!

You might also like