0% found this document useful (0 votes)
21 views

Student IM Sorting Algorithm and Searching Algorithm

This instructional module provides details on sorting and searching algorithms. It includes: 1) Descriptions of bubble sort, selection sort, insertion sort, sequential search, and binary search algorithms. 2) Java code examples for implementing each algorithm. 3) Learning outcomes related to understanding, implementing, and comparing the algorithms in terms of timing and techniques.

Uploaded by

kate buyuccan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Student IM Sorting Algorithm and Searching Algorithm

This instructional module provides details on sorting and searching algorithms. It includes: 1) Descriptions of bubble sort, selection sort, insertion sort, sequential search, and binary search algorithms. 2) Java code examples for implementing each algorithm. 3) Learning outcomes related to understanding, implementing, and comparing the algorithms in terms of timing and techniques.

Uploaded by

kate buyuccan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Republic of the Philippines

NUEVA VIZCAYA STATE UNIVERSITY


Bayombong, Nueva Vizcaya
INSTRUCTIONAL MODULE
IM No.: IM-ITECC5-1S-2021-2022

College: College of Industrial Technology


Campus : Bambang Campus

DEGREE BS- Computer COURSE NO.


PROGRAM Science
SPECIALIZATION Business COURSE TITLE Data Structure and Algorithm
Analytics /
Quality
Assurance
YEAR LEVEL 3rd Year TIME FRAME 15 WK NO. 1-3 IM NO. 1
hrs.

I. UNIT TITLE/CHAPTER TITLE

I. Sorting Algorithm and Searching Algorithm

II. LESSON TITLE

1. Bubble Sort
2. Selection Sort
3. Insertion Sort
4. Sequential Search
5. Binary Search

III. LESSON OVERVIEW

This course provides a detailed description of the code, processes and timings of the basic searching
and sorting algorithms which are Bubble Sort, Selection Sort and Insertion Sort Algorithms and Binary
Search and Sequential Search Algorithm.

IV. DESIRED LEARNING OUTCOMES


1. identify and explain briefly the concepts and processes associated with the different basic
sorting algorithms.
2. implement or integrate basic sorting algorithms in simple programs to solve for simple
problems and problem sets.
3. explain the differences of the basic sorting algorithms in terms of timings and implementation
techniques.
4. define and explain briefly the processes and basic concepts associated with basic searching
algorithms.
5. explain the differences of the basic searching algorithms in terms of timings and
implementation
techniques.
6. implement or integrate basic searching algorithms in programs to solve for solutions in
problems and problem sets.

V. LESSON CONTENT
1. BUBBLE SORT ALGORITHM JAVA CODE
import java.util.Scanner;

public class CodesCracker


{
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
NVSU-FR-ICD-05-00 (081220) Page 1 of 3
Republic of the Philippines
NUEVA VIZCAYA STATE UNIVERSITY
Bayombong, Nueva Vizcaya
INSTRUCTIONAL MODULE
IM No.: IM-ITECC5-1S-2021-2022

System.out.print("Enter the Size: ");


int n = s.nextInt();

int[] arr = new int[n];

System.out.print("Enter " +n+" Elements in Random Order: ");


for(int i=0; i<n; i++)
arr[i] = s.nextInt();

System.out.println("\nSorting the array...");


for(int i=0; i<(n-1); i++)
{
for(int j=0; j<(n-i-1); j++)
{
if(arr[j]>arr[j+1])
{
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
System.out.print("\nStep No." +(i+1)+ " -> ");
for(int j=0; j<n; j++)
System.out.print(arr[j]+ " ");
}
System.out.println("\n\nThe array is sorted successfully!");

System.out.println("\nThe new sorted array is:");


for(int i=0; i<n; i++)
System.out.print(arr[i]+ " ");
}
}

2. SELECTION SORT ALGORITHM JAVA CODE


import java.util.*;
class Main
{
static void sel_sort(int numArray[])
{
int n = numArray.length;

// traverse unsorted array


for (int i = 0; i < n-1; i++)
{
// Find the minimum element in unsorted array
int min_idx = i;
for (int j = i+1; j < n; j++)
if (numArray[j] < numArray[min_idx])
min_idx = j;

// swap minimum element with compared element


int temp = numArray[min_idx];
numArray[min_idx] = numArray[i];
numArray[i] = temp;
}
}

public static void main(String args[])


{
NVSU-FR-ICD-05-00 (081220) Page 2 of 3
Republic of the Philippines
NUEVA VIZCAYA STATE UNIVERSITY
Bayombong, Nueva Vizcaya
INSTRUCTIONAL MODULE
IM No.: IM-ITECC5-1S-2021-2022

//declare and print the original array


int numArray[] = {7,5,2,20,42,15,23,34,10};
System.out.println("Original Array:" + Arrays.toString(numArray));
//call selection sort routine
sel_sort(numArray);
//print the sorted array
System.out.println("Sorted Array:" + Arrays.toString(numArray));
}
}

3. INSERTION SORT ALGORITHM JAVA CODE


import java.util.Scanner;

public class CodesCracker


{
public static void main(String[] args)
{
int n, i, j, element;
Scanner scan = new Scanner(System.in);

System.out.print("Enter the Size of Array: ");


n = scan.nextInt();
int[] arr = new int[n];
System.out.print("Enter " +n+ " Elements: ");
for(i=0; i<n; i++)
arr[i] = scan.nextInt();

for(i=1; i<n; i++)


{
element = arr[i];

for(j=(i-1); j>=0 && arr[j]>element; j--)


arr[j+1] = arr[j];

arr[j+1] = element;
}

System.out.println("\nThe new sorted array is: ");


for(i=0; i<n; i++)
System.out.print(arr[i]+ " ");
}
}

4. SEQUENTIAL SEARCH JAVA CODE


public class LinearSearch {
public static void main(String args[]){
int array[] = {10, 20, 25, 63, 96, 57};
int size = array.length;
int value = 63;

for (int i=0 ;i< size-1; i++){


if(array[i]==value){
System.out.println("Element found index is :"+ i);
}else{
System.out.println("Element not found");
}
}
}
}

NVSU-FR-ICD-05-00 (081220) Page 3 of 3


Republic of the Philippines
NUEVA VIZCAYA STATE UNIVERSITY
Bayombong, Nueva Vizcaya
INSTRUCTIONAL MODULE
IM No.: IM-ITECC5-1S-2021-2022

5. BINARY SEARCH JAVA CODE


import java.util.*;
class Main{
public static void main(String args[]){
int numArray[] = {5,10,15,20,25,30,35};
System.out.println("The input array: " + Arrays.toString(numArray));
int key = 20;
System.out.println("\nKey to be searched=" + key);
int first = 0;
int last=numArray.length-1;
int mid = (first + last)/2;
while( first <= last ){
if ( numArray[mid] < key ){
first = mid + 1;
}else if ( numArray[mid] == key ){
System.out.println("Element is found at index: " + mid);
break;
}else{
last = mid - 1;
}
mid = (first + last)/2;
}
if ( first > last ){
System.out.println("Element is not found!");
}
}
}

VI. REFERENCES

GOOGLE

Numbering the IM No.: IM-CCCCCC-SSSSSS-NNNN-NNNN

School Year
Semester
Course Number
e.g.:
IM-COURSE NO-SEMESTER-SCHOOL YEAR
IM-MCB180-1STSEM-2020-2021

NVSU-FR-ICD-05-00 (081220) Page 4 of 3

You might also like