9.  Show all the passes in the execution of the SelectionSort on the following set : { 20, 32, 50, 19, 10, 5, 8, 12};

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

9.  Show all the passes in the execution of the SelectionSort on the following set :

{ 20, 32, 50, 19, 10, 5, 8, 12};

Expert Solution
Step 1

 

In selection sort algorithm,the lowest element  is choosen and arrange it to the proper location and  swap the current element with the next lowest number and so on.

Given set is :  {20,32,50,19,10,5,8,12} Selection sort can be carried out as follows:

      Given:      20 ,32,50,19,10,5,8,12

                  iteration 1:  20 32 50 19 10 5 8 12 
                  iteration 2:  5 32 50 19 10 20 8 12 
                  iteration 3:  5 8 50 19 10 20 32 12 
                  iteration 4:  5 8 10 19 50 20 32 12 
                  iteration : 5 8 10 12 50 20 32 19 
                  iteration 6: 5 8 10 12 19 20 32 50 
                  iteration  7:5 8 10 12 19 20 32 50 
                  iteration 8:5 8 10 12 19 20 32 50 
                   

                     After Selection Sort
                     5 8 10 12 19 20 32 50  

       Program plan:

  • include necessary header files
  • create a class  named SelectionSort
  • create a public static method selectionSort()
  • consider an array to store integer values
  • check for the necessary conditions and search for lowest element and store it in the array
  • In java there is inbuilt method  selectionSort() use that for sorting.
  • Each iteration is displays as for loop is used
  • Finally we print the sorted array.

 

 

Step 2

Program:

package com.sds.javabasic;

//create a class

public class SelectionSort{ 

    public static void selectionSort(int[] arr){ 

        for (int i = 0; i < arr.length - 1; i++) 

        { 

            int index = i; 

            for (int j = i + 1; j < arr.length; j++){ 

                if (arr[j] < arr[index]){ 

                    index = j;//searching for lowest index 

                } 

            } 

            int smallerNumber = arr[index];  

            arr[index] = arr[i]; 

            arr[i] = smallerNumber; 

           

            for(int k:arr){                     

                System.out.print(k+" ");      //displays each iteration

            }

            System.out.println();

        } 

    } 

      

    public static void main(String a[]){ 

        int[] arr1 = {20,32,50,19,10,5,8,12}; 

        System.out.println("Before Selection Sort"); 

        for(int i:arr1){ 

            System.out.print(i+" "); 

        } 

        System.out.println(); 

         

        selectionSort(arr1);   //sorting array using selection sort 

        

        System.out.println("After Selection Sort"); 

        for(int i:arr1){ 

            System.out.print(i+" "); 

        } 

    } 

}   

steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Lower bounds sorting algorithm
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education