SlideShare ist ein Scribd-Unternehmen logo
1 von 20
Arrays and Sorting
Process ,[object Object],[object Object],[object Object],[object Object]
Text file containing integers Array of short (integers) numbers.txt numbers[]
Text file containing integers Array of short (integers) numbers.txt numbers[10] 123 5 12 3 19 0 9 0 8 0 7 0 6 0 5 19 4 3 3 12 2 5 1 123 0
Text file containing integers Array of short (integers) numbers.txt numbers[10] 123 5 12 3 19 inputFromFile(inputFileName, numbersArray);   0 9 0 8 0 7 0 6 0 5 19 4 3 3 12 2 5 1 123 0
private static int inputFromFile(String filename, short[] numbers){   TextFileInput in = new TextFileInput(filename);    int lengthFilled = 0;    String line = in.readLine();   while ( lengthFilled < numbers.length && line != null ) {   numbers[lengthFilled++] = Short.parseShort(line);   line = in.readLine();    } // while    if ( line != null ) {   System.out.println(&quot;File contains too many numbers.&quot;);   System.out.println(&quot;This program can process only &quot; +   numbers.length + &quot; numbers.&quot;);   System.exit(1);    } // if    in.close();    return lengthFilled;  } // method inputFromFile   Main program calls subArrayLength = inputFromFile(inputFileName, numbersArray);  Program 1.1
Text file containing integers Array of short (integers) numbers.txt numbers[10] 123 5 19 3 12 subArrayLength is 5 0 9 0 8 0 7 0 6 0 5 12 4 3 3 19 2 5 1 123 0
numbers[10] subArrayLength 5 Partially-filled array for (int i =0;i<numbers.length; i ++) { for (int i =0;i<subArrayLength; i ++) { NO: YES: 0 9 0 8 0 7 0 6 0 5 12 4 3 3 19 2 5 1 123 0
numbers[10] subArrayLength 5 //  average the numbers sum=0; for (int i =0; i<subArrayLength; i ++) sum += numbers[i]; Average = sum/subArrayLength; 0 9 0 8 0 7 0 6 0 5 12 4 3 3 19 2 5 1 123 0
numbers[10] subArrayLength 5 //  find the smallest number smallest = numbers[0]; for (int i =1; i<subArrayLength; i ++) smallest = Math.min(smallest,numbers[i];  0 9 0 8 0 7 0 6 0 5 12 4 3 3 19 2 5 1 123 0
numbers[10] subArrayLength 5 //  find the  index  of the smallest number indexLowest = 0; for ( int j = 1; j < subArrayLength; j++ ) if ( array[j] < array[indexLowest] ) indexLowest = j;  0 9 0 8 0 7 0 6 0 5 12 4 3 3 19 2 5 1 123 0
numbers[10] //  find the  index  of the smallest number This is the basis of  “Selection Sort” 0 9 0 8 0 7 0 6 0 5 12 4 3 3 19 2 5 1 123 0
numbers[10] find the  index  of the smallest number This is the basis of  “Selection Sort” Find the smallest number and swap it with the number at the top of the array numbers[10] 0 9 0 8 0 7 0 6 0 5 19 4 3 3 12 2 5 1 123 0 0 9 0 8 0 7 0 6 0 5 19 4 123 3 12 2 5 1 3 0
numbers[10] numbers[10] sorted Not sorted; Find the smallest number here and swap it with numbers[1] 0 9 0 8 0 7 0 6 0 5 12 4 123 3 19 2 5 1 3 0
numbers[10] numbers[10] sorted 0 9 0 8 0 7 0 6 0 5 12 4 123 3 19 2 5 1 3 0 0 9 0 8 0 7 0 6 0 5 12 4 123 3 19 2 5 1 3 0
numbers[10] numbers[10] sorted 0 9 0 8 0 7 0 6 0 5 12 4 123 3 19 2 5 1 3 0 0 9 0 8 0 7 0 6 0 5 19 4 123 3 12 2 5 1 3 0
numbers[10] numbers[10] sorted 0 9 0 8 0 7 0 6 0 5 19 4 123 3 12 2 5 1 3 0 0 9 0 8 0 7 0 6 0 5 123 4 19 3 12 2 5 1 3 0
numbers[10] numbers[10] sorted 0 9 0 8 0 7 0 6 0 5 123 4 19 3 12 2 5 1 3 0 0 9 0 8 0 7 0 6 0 5 123 4 19 3 12 2 5 1 3 0
numbers[10] sorted 0 9 0 8 0 7 0 6 0 5 123 4 19 3 12 2 5 1 3 0
private static void selectionSort (short[] array, int length) {  for ( int i = 0; i < length - 1; i++ ) {  int indexLowest = i;  for ( int j = i + 1; j < length; j++ )  if ( array[j] < array[indexLowest] )  indexLowest = j; if ( array[indexLowest] != array[i] ) {  short temp = array[indexLowest]; array[indexLowest] = array[i];  array[i] = temp;  }  // if } // for i  } // method selectionSort

Weitere ähnliche Inhalte

Was ist angesagt?

Two dimensional array
Two dimensional arrayTwo dimensional array
Two dimensional arrayRajendran
 
Introduction to Array ppt
Introduction to Array pptIntroduction to Array ppt
Introduction to Array pptsandhya yadav
 
concept of Array, 1D & 2D array
concept of Array, 1D & 2D arrayconcept of Array, 1D & 2D array
concept of Array, 1D & 2D arraySangani Ankur
 
Array Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional arrayArray Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional arrayimtiazalijoono
 
Data Structure Midterm Lesson Arrays
Data Structure Midterm Lesson ArraysData Structure Midterm Lesson Arrays
Data Structure Midterm Lesson ArraysMaulen Bale
 
11 1. multi-dimensional array eng
11 1. multi-dimensional array eng11 1. multi-dimensional array eng
11 1. multi-dimensional array eng웅식 전
 
Array in c language
Array in c language Array in c language
Array in c language umesh patil
 

Was ist angesagt? (20)

Array C programming
Array C programmingArray C programming
Array C programming
 
Learn Java Part 8
Learn Java Part 8Learn Java Part 8
Learn Java Part 8
 
Array in-c
Array in-cArray in-c
Array in-c
 
Two dimensional array
Two dimensional arrayTwo dimensional array
Two dimensional array
 
Learn Java Part 9
Learn Java Part 9Learn Java Part 9
Learn Java Part 9
 
C++ programming (Array)
C++ programming (Array)C++ programming (Array)
C++ programming (Array)
 
Introduction to Array ppt
Introduction to Array pptIntroduction to Array ppt
Introduction to Array ppt
 
Lecture 16 - Multi dimensional Array
Lecture 16 - Multi dimensional ArrayLecture 16 - Multi dimensional Array
Lecture 16 - Multi dimensional Array
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
concept of Array, 1D & 2D array
concept of Array, 1D & 2D arrayconcept of Array, 1D & 2D array
concept of Array, 1D & 2D array
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
Arrays basics
Arrays basicsArrays basics
Arrays basics
 
Array Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional arrayArray Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional array
 
Lecture 15 - Array
Lecture 15 - ArrayLecture 15 - Array
Lecture 15 - Array
 
Data Structure Midterm Lesson Arrays
Data Structure Midterm Lesson ArraysData Structure Midterm Lesson Arrays
Data Structure Midterm Lesson Arrays
 
11 1. multi-dimensional array eng
11 1. multi-dimensional array eng11 1. multi-dimensional array eng
11 1. multi-dimensional array eng
 
Python_ 3 CheatSheet
Python_ 3 CheatSheetPython_ 3 CheatSheet
Python_ 3 CheatSheet
 
Arrays in C language
Arrays in C languageArrays in C language
Arrays in C language
 
Array in C
Array in CArray in C
Array in C
 
Array in c language
Array in c language Array in c language
Array in c language
 

Ähnlich wie 1 arrays and sorting

Ähnlich wie 1 arrays and sorting (20)

Lec2&3 data structure
Lec2&3 data structureLec2&3 data structure
Lec2&3 data structure
 
Lec2
Lec2Lec2
Lec2
 
Lec2&3_DataStructure
Lec2&3_DataStructureLec2&3_DataStructure
Lec2&3_DataStructure
 
Data structure array
Data structure  arrayData structure  array
Data structure array
 
Computer Programming- Lecture 8
Computer Programming- Lecture 8Computer Programming- Lecture 8
Computer Programming- Lecture 8
 
Chapter 7.1
Chapter 7.1Chapter 7.1
Chapter 7.1
 
Arrays
ArraysArrays
Arrays
 
Arrays
ArraysArrays
Arrays
 
Array assignment
Array assignmentArray assignment
Array assignment
 
Arrays
ArraysArrays
Arrays
 
Array and string in C++_093547 analysis.pptx
Array and string in C++_093547 analysis.pptxArray and string in C++_093547 analysis.pptx
Array and string in C++_093547 analysis.pptx
 
Chapter 7.4
Chapter 7.4Chapter 7.4
Chapter 7.4
 
Multi dimensional arrays
Multi dimensional arraysMulti dimensional arrays
Multi dimensional arrays
 
9781439035665 ppt ch09
9781439035665 ppt ch099781439035665 ppt ch09
9781439035665 ppt ch09
 
Lec2
Lec2Lec2
Lec2
 
Lec2&3 data structure
Lec2&3 data structureLec2&3 data structure
Lec2&3 data structure
 
An Introduction to Programming in Java: Arrays
An Introduction to Programming in Java: ArraysAn Introduction to Programming in Java: Arrays
An Introduction to Programming in Java: Arrays
 
Visual Programing basic lectures 7.pptx
Visual Programing basic lectures  7.pptxVisual Programing basic lectures  7.pptx
Visual Programing basic lectures 7.pptx
 
Arrays in C++
Arrays in C++Arrays in C++
Arrays in C++
 
2DArrays.ppt
2DArrays.ppt2DArrays.ppt
2DArrays.ppt
 

Kürzlich hochgeladen

Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DaySri Ambati
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 

Kürzlich hochgeladen (20)

Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 

1 arrays and sorting

  • 2.
  • 3. Text file containing integers Array of short (integers) numbers.txt numbers[]
  • 4. Text file containing integers Array of short (integers) numbers.txt numbers[10] 123 5 12 3 19 0 9 0 8 0 7 0 6 0 5 19 4 3 3 12 2 5 1 123 0
  • 5. Text file containing integers Array of short (integers) numbers.txt numbers[10] 123 5 12 3 19 inputFromFile(inputFileName, numbersArray); 0 9 0 8 0 7 0 6 0 5 19 4 3 3 12 2 5 1 123 0
  • 6. private static int inputFromFile(String filename, short[] numbers){ TextFileInput in = new TextFileInput(filename); int lengthFilled = 0; String line = in.readLine(); while ( lengthFilled < numbers.length && line != null ) { numbers[lengthFilled++] = Short.parseShort(line); line = in.readLine(); } // while if ( line != null ) { System.out.println(&quot;File contains too many numbers.&quot;); System.out.println(&quot;This program can process only &quot; + numbers.length + &quot; numbers.&quot;); System.exit(1); } // if in.close(); return lengthFilled; } // method inputFromFile Main program calls subArrayLength = inputFromFile(inputFileName, numbersArray); Program 1.1
  • 7. Text file containing integers Array of short (integers) numbers.txt numbers[10] 123 5 19 3 12 subArrayLength is 5 0 9 0 8 0 7 0 6 0 5 12 4 3 3 19 2 5 1 123 0
  • 8. numbers[10] subArrayLength 5 Partially-filled array for (int i =0;i<numbers.length; i ++) { for (int i =0;i<subArrayLength; i ++) { NO: YES: 0 9 0 8 0 7 0 6 0 5 12 4 3 3 19 2 5 1 123 0
  • 9. numbers[10] subArrayLength 5 // average the numbers sum=0; for (int i =0; i<subArrayLength; i ++) sum += numbers[i]; Average = sum/subArrayLength; 0 9 0 8 0 7 0 6 0 5 12 4 3 3 19 2 5 1 123 0
  • 10. numbers[10] subArrayLength 5 // find the smallest number smallest = numbers[0]; for (int i =1; i<subArrayLength; i ++) smallest = Math.min(smallest,numbers[i]; 0 9 0 8 0 7 0 6 0 5 12 4 3 3 19 2 5 1 123 0
  • 11. numbers[10] subArrayLength 5 // find the index of the smallest number indexLowest = 0; for ( int j = 1; j < subArrayLength; j++ ) if ( array[j] < array[indexLowest] ) indexLowest = j; 0 9 0 8 0 7 0 6 0 5 12 4 3 3 19 2 5 1 123 0
  • 12. numbers[10] // find the index of the smallest number This is the basis of “Selection Sort” 0 9 0 8 0 7 0 6 0 5 12 4 3 3 19 2 5 1 123 0
  • 13. numbers[10] find the index of the smallest number This is the basis of “Selection Sort” Find the smallest number and swap it with the number at the top of the array numbers[10] 0 9 0 8 0 7 0 6 0 5 19 4 3 3 12 2 5 1 123 0 0 9 0 8 0 7 0 6 0 5 19 4 123 3 12 2 5 1 3 0
  • 14. numbers[10] numbers[10] sorted Not sorted; Find the smallest number here and swap it with numbers[1] 0 9 0 8 0 7 0 6 0 5 12 4 123 3 19 2 5 1 3 0
  • 15. numbers[10] numbers[10] sorted 0 9 0 8 0 7 0 6 0 5 12 4 123 3 19 2 5 1 3 0 0 9 0 8 0 7 0 6 0 5 12 4 123 3 19 2 5 1 3 0
  • 16. numbers[10] numbers[10] sorted 0 9 0 8 0 7 0 6 0 5 12 4 123 3 19 2 5 1 3 0 0 9 0 8 0 7 0 6 0 5 19 4 123 3 12 2 5 1 3 0
  • 17. numbers[10] numbers[10] sorted 0 9 0 8 0 7 0 6 0 5 19 4 123 3 12 2 5 1 3 0 0 9 0 8 0 7 0 6 0 5 123 4 19 3 12 2 5 1 3 0
  • 18. numbers[10] numbers[10] sorted 0 9 0 8 0 7 0 6 0 5 123 4 19 3 12 2 5 1 3 0 0 9 0 8 0 7 0 6 0 5 123 4 19 3 12 2 5 1 3 0
  • 19. numbers[10] sorted 0 9 0 8 0 7 0 6 0 5 123 4 19 3 12 2 5 1 3 0
  • 20. private static void selectionSort (short[] array, int length) { for ( int i = 0; i < length - 1; i++ ) { int indexLowest = i; for ( int j = i + 1; j < length; j++ ) if ( array[j] < array[indexLowest] ) indexLowest = j; if ( array[indexLowest] != array[i] ) { short temp = array[indexLowest]; array[indexLowest] = array[i]; array[i] = temp; } // if } // for i } // method selectionSort