SlideShare a Scribd company logo
CSE110
Principles of Programming
with Java
Lecture 29:
Sorting
Javier Gonzalez-Sanchez
javiergs@asu.edu
javiergs.engineering.asu.edu
Office Hours: By appointment
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 2
Bubble sort
public static void bubbleSort(int[] array1) {
boolean swap;
do {
swap = false;

for (int index=0;index<=array1.length-2;index++) {
// compare element at index and at index+1
if (array1[index] > array1[index+1])
{
//swap them if they are not in the order
int temp = array1[index+1];
array1[index+1] = array1[index];
array1[index] = temp;

swap = true
}
}
} while (swap == true);
}
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 3
More Efficient Bubble sort
• Notice that after the 1st pass, the largest element is
placed in its final position (right most location).
• That indicates that after the 2nd pass, the second
largest element will be placed in its final position
and so on.
• So we can reduce the number of comparisons by 1
for every pass.
• If we have n elements in the array, then the 1st pass
requires (n-1) comparisons, the second pass
requires (n-2) comparisons, and so on.
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 4
More Efficient Bubble sort
//More efficient version of Bubble Sort
public static void bubbleSort2(int[ ] array1) {
for(int position=array1.length-2; position>=0; position--) {
for (int index=0; index<=position; index++) {
//compare the element at index and at index+1
if (array1[index] > array1[index+1]){
//swap them if they are not in the order
int temp = array1[index+1];
array1[index+1] = array1[index];
array1[index] = temp;
}
}
}
}
Sorting
Selection Sort
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 6
Algorithm
1. Selection Sort locates the smallest element in the
array and exchanges it with the element in position
0.
2. It locates the next smallest element in the array
and exchanges it with the element in position 1.
3. It continues until all elements are in order.
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 7
Example 1
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 8
Example 1
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 9
Example 2
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 10
Selection Sort
public static void selectionSort (int[] data) {
int minIndex;
for (int index=0; index<data.length-1; index++) {
minIndex = index;
for (int scan=index+1;scan<data.length; scan++){
if (data[scan] < data[minIndex]) {
minIndex = scan;
}
}
//swap the two elements, data[minIndex] and data[index]
int temp = data[minIndex];
data[minIndex] = data[index];
data[index] = temp;
}
}
CSE110 - Principles of Programming
Javier Gonzalez-Sanchez
javiergs@asu.edu
Summer 2017
Disclaimer. These slides can only be used as study material for the class CSE110 at ASU. They cannot be distributed or used for another purpose.

More Related Content

PDF
Multiple regression with R
PDF
L35 b-prop&ops-limits1preview
PPTX
Python Lecture 5
PDF
Shape functions
PPTX
Gradient Boosting
PPTX
Hidden Markov Model
PDF
Physics Tutor Singapore
PPTX
Activity selection problem
Multiple regression with R
L35 b-prop&ops-limits1preview
Python Lecture 5
Shape functions
Gradient Boosting
Hidden Markov Model
Physics Tutor Singapore
Activity selection problem

What's hot (20)

PPTX
Activity selection problem
PPT
5.2 Strong Induction
PPT
Chapter I
PPTX
MARRIOTT ROOMS FORECASTING Operation management assignment (1)
PDF
PM [B09] The Trigo Companions
PPTX
Bfs dfs mst
PDF
Math induction principle (slides)
PPTX
Evaluation of prefix expression with example
PPT
Lecture13 motion
PDF
Engineering Mechanics categories of equilibrium in two dimensions
PDF
Algebra 2 Section 2-4
PDF
Continuity of a function module02
PDF
Post_Number Systems_2
PDF
Engineering Mechanics categories of equilibrium in three dimensions
PDF
Lesson 19: Related Rates
PPTX
Rearranging Formulas
PPTX
Vector
PDF
Activity selection problem
5.2 Strong Induction
Chapter I
MARRIOTT ROOMS FORECASTING Operation management assignment (1)
PM [B09] The Trigo Companions
Bfs dfs mst
Math induction principle (slides)
Evaluation of prefix expression with example
Lecture13 motion
Engineering Mechanics categories of equilibrium in two dimensions
Algebra 2 Section 2-4
Continuity of a function module02
Post_Number Systems_2
Engineering Mechanics categories of equilibrium in three dimensions
Lesson 19: Related Rates
Rearranging Formulas
Vector
Ad

Similar to 201707 CSE110 Lecture 29 (20)

PDF
201707 CSE110 Lecture 30
PDF
201707 CSE110 Lecture 28
PPT
07-sorting.ppt for insertion sort and bubble sorting technquies
PDF
Advanced Topics In Java Core Concepts In Data Structures Noel Kalicharan
PPT
Decimal Long Double Double Double. Represents double-precision floating-point...
PPTX
Searching and sorting Techniques in Data structures
PPT
lecture to proide studeny with F-k-sorting.ppt
PDF
PPT
lecture-k-sorting.ppt
PPT
Lecture k-sorting
PPT
14-sorting (3).ppt
PPT
14-sorting.ppt
PPT
14-sorting.ppt
PPT
14-sorting.ppt
PDF
L 14-ct1120
PPT
Sorting algorithms
PPT
simple-sorting algorithms
PPT
Sorted Algorithm - The data to be sorted is all stored in the computer’s main...
PPTX
A Comprehensive Comparative Study and Performance Evaluation
201707 CSE110 Lecture 30
201707 CSE110 Lecture 28
07-sorting.ppt for insertion sort and bubble sorting technquies
Advanced Topics In Java Core Concepts In Data Structures Noel Kalicharan
Decimal Long Double Double Double. Represents double-precision floating-point...
Searching and sorting Techniques in Data structures
lecture to proide studeny with F-k-sorting.ppt
lecture-k-sorting.ppt
Lecture k-sorting
14-sorting (3).ppt
14-sorting.ppt
14-sorting.ppt
14-sorting.ppt
L 14-ct1120
Sorting algorithms
simple-sorting algorithms
Sorted Algorithm - The data to be sorted is all stored in the computer’s main...
A Comprehensive Comparative Study and Performance Evaluation
Ad

More from Javier Gonzalez-Sanchez (20)

PDF
201804 SER332 Lecture 01
PDF
201801 SER332 Lecture 03
PDF
201801 SER332 Lecture 04
PDF
201801 SER332 Lecture 02
PDF
201801 CSE240 Lecture 26
PDF
201801 CSE240 Lecture 25
PDF
201801 CSE240 Lecture 24
PDF
201801 CSE240 Lecture 23
PDF
201801 CSE240 Lecture 22
PDF
201801 CSE240 Lecture 21
PDF
201801 CSE240 Lecture 20
PDF
201801 CSE240 Lecture 19
PDF
201801 CSE240 Lecture 18
PDF
201801 CSE240 Lecture 17
PDF
201801 CSE240 Lecture 16
PDF
201801 CSE240 Lecture 15
PDF
201801 CSE240 Lecture 14
PDF
201801 CSE240 Lecture 13
PDF
201801 CSE240 Lecture 12
PDF
201801 CSE240 Lecture 11
201804 SER332 Lecture 01
201801 SER332 Lecture 03
201801 SER332 Lecture 04
201801 SER332 Lecture 02
201801 CSE240 Lecture 26
201801 CSE240 Lecture 25
201801 CSE240 Lecture 24
201801 CSE240 Lecture 23
201801 CSE240 Lecture 22
201801 CSE240 Lecture 21
201801 CSE240 Lecture 20
201801 CSE240 Lecture 19
201801 CSE240 Lecture 18
201801 CSE240 Lecture 17
201801 CSE240 Lecture 16
201801 CSE240 Lecture 15
201801 CSE240 Lecture 14
201801 CSE240 Lecture 13
201801 CSE240 Lecture 12
201801 CSE240 Lecture 11

Recently uploaded (20)

PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPT
Teaching material agriculture food technology
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Electronic commerce courselecture one. Pdf
PDF
Machine learning based COVID-19 study performance prediction
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Encapsulation theory and applications.pdf
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
Cloud computing and distributed systems.
PPTX
Big Data Technologies - Introduction.pptx
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Unlocking AI with Model Context Protocol (MCP)
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Dropbox Q2 2025 Financial Results & Investor Presentation
“AI and Expert System Decision Support & Business Intelligence Systems”
Teaching material agriculture food technology
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
MIND Revenue Release Quarter 2 2025 Press Release
Electronic commerce courselecture one. Pdf
Machine learning based COVID-19 study performance prediction
MYSQL Presentation for SQL database connectivity
Encapsulation theory and applications.pdf
Mobile App Security Testing_ A Comprehensive Guide.pdf
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Cloud computing and distributed systems.
Big Data Technologies - Introduction.pptx
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
The Rise and Fall of 3GPP – Time for a Sabbatical?
Reach Out and Touch Someone: Haptics and Empathic Computing
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Unlocking AI with Model Context Protocol (MCP)

201707 CSE110 Lecture 29

  • 1. CSE110 Principles of Programming with Java Lecture 29: Sorting Javier Gonzalez-Sanchez javiergs@asu.edu javiergs.engineering.asu.edu Office Hours: By appointment
  • 2. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 2 Bubble sort public static void bubbleSort(int[] array1) { boolean swap; do { swap = false;
 for (int index=0;index<=array1.length-2;index++) { // compare element at index and at index+1 if (array1[index] > array1[index+1])
{ //swap them if they are not in the order int temp = array1[index+1]; array1[index+1] = array1[index]; array1[index] = temp;
 swap = true } } } while (swap == true); }
  • 3. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 3 More Efficient Bubble sort • Notice that after the 1st pass, the largest element is placed in its final position (right most location). • That indicates that after the 2nd pass, the second largest element will be placed in its final position and so on. • So we can reduce the number of comparisons by 1 for every pass. • If we have n elements in the array, then the 1st pass requires (n-1) comparisons, the second pass requires (n-2) comparisons, and so on.
  • 4. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 4 More Efficient Bubble sort //More efficient version of Bubble Sort public static void bubbleSort2(int[ ] array1) { for(int position=array1.length-2; position>=0; position--) { for (int index=0; index<=position; index++) { //compare the element at index and at index+1 if (array1[index] > array1[index+1]){ //swap them if they are not in the order int temp = array1[index+1]; array1[index+1] = array1[index]; array1[index] = temp; } } } }
  • 6. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 6 Algorithm 1. Selection Sort locates the smallest element in the array and exchanges it with the element in position 0. 2. It locates the next smallest element in the array and exchanges it with the element in position 1. 3. It continues until all elements are in order.
  • 7. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 7 Example 1
  • 8. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 8 Example 1
  • 9. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 9 Example 2
  • 10. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 10 Selection Sort public static void selectionSort (int[] data) { int minIndex; for (int index=0; index<data.length-1; index++) { minIndex = index; for (int scan=index+1;scan<data.length; scan++){ if (data[scan] < data[minIndex]) { minIndex = scan; } } //swap the two elements, data[minIndex] and data[index] int temp = data[minIndex]; data[minIndex] = data[index]; data[index] = temp; } }
  • 11. CSE110 - Principles of Programming Javier Gonzalez-Sanchez javiergs@asu.edu Summer 2017 Disclaimer. These slides can only be used as study material for the class CSE110 at ASU. They cannot be distributed or used for another purpose.