SlideShare uma empresa Scribd logo
1 de 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

Mais conteúdo relacionado

Mais procurados

Data Structure Midterm Lesson Arrays
Data Structure Midterm Lesson ArraysData Structure Midterm Lesson Arrays
Data Structure Midterm Lesson Arrays
Maulen Bale
 
11 1. multi-dimensional array eng
11 1. multi-dimensional array eng11 1. multi-dimensional array eng
11 1. multi-dimensional array eng
웅식 전
 

Mais procurados (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
 

Semelhante a 1 arrays and sorting

Lec2&3 data structure
Lec2&3 data structureLec2&3 data structure
Lec2&3 data structure
Saad Gabr
 
Chapter 7.1
Chapter 7.1Chapter 7.1
Chapter 7.1
sotlsoc
 
Chapter 7.4
Chapter 7.4Chapter 7.4
Chapter 7.4
sotlsoc
 
Multi dimensional arrays
Multi dimensional arraysMulti dimensional arrays
Multi dimensional arrays
Aseelhalees
 
9781439035665 ppt ch09
9781439035665 ppt ch099781439035665 ppt ch09
9781439035665 ppt ch09
Terry Yoast
 
2DArrays.ppt
2DArrays.ppt2DArrays.ppt
2DArrays.ppt
Nooryaseen9
 

Semelhante a 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&3 data structure
Lec2&3 data structureLec2&3 data structure
Lec2&3 data structure
 
Lec2
Lec2Lec2
Lec2
 
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
 

Último

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 

Último (20)

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
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?
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
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...
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
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
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 

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