SlideShare a Scribd company logo
1 of 10
CSE110
Principles of Programming
with Java
Lecture 30:
Sorting and Searching – Arrays of Objects
Javier Gonzalez-Sanchez
javiergs@asu.edu
javiergs.engineering.asu.edu
Office Hours: By appointment
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 2
Previously - Selection Sort
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 3
Previously - 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[index];
data[index] = data[minIndex];
data[minIndex] = temp;
}
}
Sorting Arrays of Objects
JavierGonzalez-Sanchez|CSE110|Summer2017|5
Arrays of Objects
Student [] array = new Student[4];
array[0] = new Student(”Robert”, 4);
array[1] = new Student(”Mary”, 1);
array[2] = new Student(”Alice”, 3);
array[3] = new Student(”John”, 2);
JohnMary AliceRobert
4 1 3 2
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 6
Arrays of Objects | swap
// int temp = data[index_1];
// data[index_1] = data[index_2];
// data[index_2] = temp;
How to do this for arrays of objects?
JohnMary AliceRobert
4 1 3 2
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 7
Swap | Arrays of Objects
What about
Sort (Bubble or Selection)?
Search (Linear or Binary)?
JohnMary AliceRobert
4 1 3 2
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 8
What about “Selection Sort” for A of O?
// Do changes in the highlighted places
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;
}
}
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 9
What about “Binary Search” for A of O?
// Do changes in the highlighted places
public static int binarySearch (int[] data, int target) {
int start = 0, end = data.length-1, middle;
int position = -1; //return -1 if not found
boolean found = false;
while (found == false && start <= end) {
middle = (start+end)/2;
if (data[middle] == target){
found = true;
position = middle;
} else if (data[middle] > target)
end = middle - 1;
else
start = middle + 1;
}
return position; //return -1 if not found
}
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

What's hot

Mdst3559 2011-04-26-viz1
Mdst3559 2011-04-26-viz1Mdst3559 2011-04-26-viz1
Mdst3559 2011-04-26-viz1
Rafael Alvarado
 
07 Retrieving Objects
07 Retrieving Objects07 Retrieving Objects
07 Retrieving Objects
Ranjan Kumar
 

What's hot (18)

Machine Learning in R
Machine Learning in RMachine Learning in R
Machine Learning in R
 
K-means Clustering with Scikit-Learn
K-means Clustering with Scikit-LearnK-means Clustering with Scikit-Learn
K-means Clustering with Scikit-Learn
 
WF ED 540, Class Meeting 2 - Identifying & converting data types, 2016
WF ED 540, Class Meeting 2 - Identifying & converting data types, 2016WF ED 540, Class Meeting 2 - Identifying & converting data types, 2016
WF ED 540, Class Meeting 2 - Identifying & converting data types, 2016
 
Mdst3559 2011-04-26-viz1
Mdst3559 2011-04-26-viz1Mdst3559 2011-04-26-viz1
Mdst3559 2011-04-26-viz1
 
K-Means Algorithm Implementation In python
K-Means Algorithm Implementation In pythonK-Means Algorithm Implementation In python
K-Means Algorithm Implementation In python
 
Developing an Expression Language for Quantitative Financial Modeling
Developing an Expression Language for Quantitative Financial ModelingDeveloping an Expression Language for Quantitative Financial Modeling
Developing an Expression Language for Quantitative Financial Modeling
 
Unit 2 dsa LINEAR DATA STRUCTURE
Unit 2 dsa LINEAR DATA STRUCTUREUnit 2 dsa LINEAR DATA STRUCTURE
Unit 2 dsa LINEAR DATA STRUCTURE
 
Java arrays
Java arraysJava arrays
Java arrays
 
Program Pengurutan Data Dengan Buble Sort
Program Pengurutan Data Dengan Buble SortProgram Pengurutan Data Dengan Buble Sort
Program Pengurutan Data Dengan Buble Sort
 
Quick dive to pandas
Quick dive to pandasQuick dive to pandas
Quick dive to pandas
 
Mergesort
MergesortMergesort
Mergesort
 
Postgresql index-expression
Postgresql index-expressionPostgresql index-expression
Postgresql index-expression
 
Postgresql İndex on Expression
Postgresql İndex on ExpressionPostgresql İndex on Expression
Postgresql İndex on Expression
 
Efficient frequent pattern mining in distributed system
Efficient frequent pattern mining in distributed systemEfficient frequent pattern mining in distributed system
Efficient frequent pattern mining in distributed system
 
Do something in 5 with gas 4- Get your analytics profiles to a spreadsheet
Do something in 5 with gas 4- Get your analytics profiles to a spreadsheetDo something in 5 with gas 4- Get your analytics profiles to a spreadsheet
Do something in 5 with gas 4- Get your analytics profiles to a spreadsheet
 
NoSQL
NoSQLNoSQL
NoSQL
 
07 Retrieving Objects
07 Retrieving Objects07 Retrieving Objects
07 Retrieving Objects
 
Algorithms python arraylistmatrix
Algorithms python arraylistmatrixAlgorithms python arraylistmatrix
Algorithms python arraylistmatrix
 

Similar to 201707 CSE110 Lecture 30

Полнотекстовый поиск в PostgreSQL за миллисекунды (Олег Бартунов, Александр К...
Полнотекстовый поиск в PostgreSQL за миллисекунды (Олег Бартунов, Александр К...Полнотекстовый поиск в PostgreSQL за миллисекунды (Олег Бартунов, Александр К...
Полнотекстовый поиск в PostgreSQL за миллисекунды (Олег Бартунов, Александр К...
Ontico
 
Questions has 4 parts.1st part Program to implement sorting algor.pdf
Questions has 4 parts.1st part Program to implement sorting algor.pdfQuestions has 4 parts.1st part Program to implement sorting algor.pdf
Questions has 4 parts.1st part Program to implement sorting algor.pdf
apexelectronices01
 

Similar to 201707 CSE110 Lecture 30 (20)

201707 CSE110 Lecture 29
201707 CSE110 Lecture 29201707 CSE110 Lecture 29
201707 CSE110 Lecture 29
 
Splunk Ninjas: New Features and Search Dojo
Splunk Ninjas: New Features and Search DojoSplunk Ninjas: New Features and Search Dojo
Splunk Ninjas: New Features and Search Dojo
 
Sequential & binary, linear search
Sequential & binary, linear searchSequential & binary, linear search
Sequential & binary, linear search
 
Advance data structure & algorithm
Advance data structure & algorithmAdvance data structure & algorithm
Advance data structure & algorithm
 
Unit iv(dsc++)
Unit iv(dsc++)Unit iv(dsc++)
Unit iv(dsc++)
 
Computer Science Practical Science C++ with SQL commands
Computer Science Practical Science C++ with SQL commandsComputer Science Practical Science C++ with SQL commands
Computer Science Practical Science C++ with SQL commands
 
Main ds manual
Main ds manualMain ds manual
Main ds manual
 
DN 2017 | Multi-Paradigm Data Science - On the many dimensions of Knowledge D...
DN 2017 | Multi-Paradigm Data Science - On the many dimensions of Knowledge D...DN 2017 | Multi-Paradigm Data Science - On the many dimensions of Knowledge D...
DN 2017 | Multi-Paradigm Data Science - On the many dimensions of Knowledge D...
 
Kmeans plusplus
Kmeans plusplusKmeans plusplus
Kmeans plusplus
 
Chapter 4: basic search algorithms data structure
Chapter 4: basic search algorithms data structureChapter 4: basic search algorithms data structure
Chapter 4: basic search algorithms data structure
 
Feature Engineering - Getting most out of data for predictive models
Feature Engineering - Getting most out of data for predictive modelsFeature Engineering - Getting most out of data for predictive models
Feature Engineering - Getting most out of data for predictive models
 
201707 CSE110 Lecture 14
201707 CSE110 Lecture 14   201707 CSE110 Lecture 14
201707 CSE110 Lecture 14
 
MongoDB.local DC 2018: Tips and Tricks for Avoiding Common Query Pitfalls
MongoDB.local DC 2018: Tips and Tricks for Avoiding Common Query PitfallsMongoDB.local DC 2018: Tips and Tricks for Avoiding Common Query Pitfalls
MongoDB.local DC 2018: Tips and Tricks for Avoiding Common Query Pitfalls
 
UNIT IV -Data Structures.pdf
UNIT IV -Data Structures.pdfUNIT IV -Data Structures.pdf
UNIT IV -Data Structures.pdf
 
PyData Paris 2015 - Track 1.2 Gilles Louppe
PyData Paris 2015 - Track 1.2 Gilles LouppePyData Paris 2015 - Track 1.2 Gilles Louppe
PyData Paris 2015 - Track 1.2 Gilles Louppe
 
Полнотекстовый поиск в PostgreSQL за миллисекунды (Олег Бартунов, Александр К...
Полнотекстовый поиск в PostgreSQL за миллисекунды (Олег Бартунов, Александр К...Полнотекстовый поиск в PostgreSQL за миллисекунды (Олег Бартунов, Александр К...
Полнотекстовый поиск в PostgreSQL за миллисекунды (Олег Бартунов, Александр К...
 
Sorting techniques
Sorting techniques Sorting techniques
Sorting techniques
 
Questions has 4 parts.1st part Program to implement sorting algor.pdf
Questions has 4 parts.1st part Program to implement sorting algor.pdfQuestions has 4 parts.1st part Program to implement sorting algor.pdf
Questions has 4 parts.1st part Program to implement sorting algor.pdf
 
Multi faceted responsive search, autocomplete, feeds engine & logging
Multi faceted responsive search, autocomplete, feeds engine & loggingMulti faceted responsive search, autocomplete, feeds engine & logging
Multi faceted responsive search, autocomplete, feeds engine & logging
 
201707 CSE110 Lecture 34
201707 CSE110 Lecture 34   201707 CSE110 Lecture 34
201707 CSE110 Lecture 34
 

More from Javier Gonzalez-Sanchez

More from Javier Gonzalez-Sanchez (20)

201804 SER332 Lecture 01
201804 SER332 Lecture 01201804 SER332 Lecture 01
201804 SER332 Lecture 01
 
201801 SER332 Lecture 03
201801 SER332 Lecture 03201801 SER332 Lecture 03
201801 SER332 Lecture 03
 
201801 SER332 Lecture 04
201801 SER332 Lecture 04201801 SER332 Lecture 04
201801 SER332 Lecture 04
 
201801 SER332 Lecture 02
201801 SER332 Lecture 02201801 SER332 Lecture 02
201801 SER332 Lecture 02
 
201801 CSE240 Lecture 26
201801 CSE240 Lecture 26201801 CSE240 Lecture 26
201801 CSE240 Lecture 26
 
201801 CSE240 Lecture 25
201801 CSE240 Lecture 25201801 CSE240 Lecture 25
201801 CSE240 Lecture 25
 
201801 CSE240 Lecture 24
201801 CSE240 Lecture 24201801 CSE240 Lecture 24
201801 CSE240 Lecture 24
 
201801 CSE240 Lecture 23
201801 CSE240 Lecture 23201801 CSE240 Lecture 23
201801 CSE240 Lecture 23
 
201801 CSE240 Lecture 22
201801 CSE240 Lecture 22201801 CSE240 Lecture 22
201801 CSE240 Lecture 22
 
201801 CSE240 Lecture 21
201801 CSE240 Lecture 21201801 CSE240 Lecture 21
201801 CSE240 Lecture 21
 
201801 CSE240 Lecture 20
201801 CSE240 Lecture 20201801 CSE240 Lecture 20
201801 CSE240 Lecture 20
 
201801 CSE240 Lecture 19
201801 CSE240 Lecture 19201801 CSE240 Lecture 19
201801 CSE240 Lecture 19
 
201801 CSE240 Lecture 18
201801 CSE240 Lecture 18201801 CSE240 Lecture 18
201801 CSE240 Lecture 18
 
201801 CSE240 Lecture 17
201801 CSE240 Lecture 17201801 CSE240 Lecture 17
201801 CSE240 Lecture 17
 
201801 CSE240 Lecture 16
201801 CSE240 Lecture 16201801 CSE240 Lecture 16
201801 CSE240 Lecture 16
 
201801 CSE240 Lecture 15
201801 CSE240 Lecture 15201801 CSE240 Lecture 15
201801 CSE240 Lecture 15
 
201801 CSE240 Lecture 14
201801 CSE240 Lecture 14201801 CSE240 Lecture 14
201801 CSE240 Lecture 14
 
201801 CSE240 Lecture 13
201801 CSE240 Lecture 13201801 CSE240 Lecture 13
201801 CSE240 Lecture 13
 
201801 CSE240 Lecture 12
201801 CSE240 Lecture 12201801 CSE240 Lecture 12
201801 CSE240 Lecture 12
 
201801 CSE240 Lecture 11
201801 CSE240 Lecture 11201801 CSE240 Lecture 11
201801 CSE240 Lecture 11
 

Recently uploaded

Recently uploaded (20)

GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 

201707 CSE110 Lecture 30

  • 1. CSE110 Principles of Programming with Java Lecture 30: Sorting and Searching – Arrays of Objects Javier Gonzalez-Sanchez javiergs@asu.edu javiergs.engineering.asu.edu Office Hours: By appointment
  • 2. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 2 Previously - Selection Sort
  • 3. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 3 Previously - 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[index]; data[index] = data[minIndex]; data[minIndex] = temp; } }
  • 5. JavierGonzalez-Sanchez|CSE110|Summer2017|5 Arrays of Objects Student [] array = new Student[4]; array[0] = new Student(”Robert”, 4); array[1] = new Student(”Mary”, 1); array[2] = new Student(”Alice”, 3); array[3] = new Student(”John”, 2); JohnMary AliceRobert 4 1 3 2
  • 6. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 6 Arrays of Objects | swap // int temp = data[index_1]; // data[index_1] = data[index_2]; // data[index_2] = temp; How to do this for arrays of objects? JohnMary AliceRobert 4 1 3 2
  • 7. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 7 Swap | Arrays of Objects What about Sort (Bubble or Selection)? Search (Linear or Binary)? JohnMary AliceRobert 4 1 3 2
  • 8. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 8 What about “Selection Sort” for A of O? // Do changes in the highlighted places 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; } }
  • 9. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 9 What about “Binary Search” for A of O? // Do changes in the highlighted places public static int binarySearch (int[] data, int target) { int start = 0, end = data.length-1, middle; int position = -1; //return -1 if not found boolean found = false; while (found == false && start <= end) { middle = (start+end)/2; if (data[middle] == target){ found = true; position = middle; } else if (data[middle] > target) end = middle - 1; else start = middle + 1; } return position; //return -1 if not found }
  • 10. 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.