0% found this document useful (0 votes)
12 views6 pages

Struktur Data 3.4 Fariel

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views6 pages

Struktur Data 3.4 Fariel

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

LAPORAN PRAKTIKUM

STRUKTUR DATA 3.4

Disusun Oleh:
Fariel Dzaky Yugisaputra C2C023053

PRODI S1 INFORMATIKA
UNIVERSITAS MUHAMMADIYAH SEMARANG
2023
KATA PENGANTAR

Puji syukur kami panjatkan ke hadirat Tuhan Yang Maha Esa, karena atas
rahmat dan hidayah-Nya, kami dapat menyelesaikan laporan praktikum ini. Laporan
ini disusun sebagai salah satu tugas mata kuliah Praktikum Struktur Data yang kami
ikuti pada semester ini.
Ucapan terima kasih saya sampaikan kepada bapak dosen pengampu mata
kuliah praktikum algoritma dan pemrograman yang telah membantu memberikan
arahan dan pemahaman dalam penyusunan laporan ini.
Tentunya, Saya menyadari bahwa laporan ini masih jauh dari sempurna. Oleh
karena itu, kritik dan saran yang membangun sangat Saya harapkan guna perbaikan di
masa yang akan datang. Akhir kata, Semoga apa yang ditulis dapat bermanfaat bagi
semua pihak yang membutuhkan

Semarang, 9 November 2024

Fariel Dzaky Yugisaputra


3.4 Sorting and Searching

1. Program for Storing, Searching, and Sorting Numeric Values


a. Create a project:
 Name: sortAndSearch.
b. Integer Array:
 Create an integer array numbers that can hold 50 values.
Code Implementation:
java
Salin kode
import java.util.Random;
import java.util.Scanner;

public class SortAndSearch {

public static void main(String[] args) {


int[] numbers = new int[50];
Random rand = new Random();

// Fill array with random numbers between 0 and 100


for (int i = 0; i < numbers.length; i++) {
numbers[i] = rand.nextInt(101); // 0 to 100 inclusive
}

// Display unordered list


System.out.println("Unordered list:");
displayArray(numbers);

// Get number to search from user


Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number to search for: ");
int searchValue = scanner.nextInt();

// Perform a linear search


int position = linearSearch(numbers, searchValue);
if (position != -1) {
System.out.println("Number found at position: " + position);
} else {
System.out.println("Number not found.");
}

// Sort array using bubble sort


bubbleSort(numbers);

// Display ordered list


System.out.println("\nOrdered list:");
displayArray(numbers);

// Search again in the sorted array


position = linearSearch(numbers, searchValue);
if (position != -1) {
System.out.println("Number found at position: " + position);
} else {
System.out.println("Number not found.");
}

scanner.close();
}

// Linear search method


public static int linearSearch(int[] array, int value) {
for (int i = 0; i < array.length; i++) {
if (array[i] == value) {
return i; // Return index
}
}
return -1; // Not found
}

// Bubble sort method


public static void bubbleSort(int[] array) {
for (int i = 0; i < array.length - 1; i++) {
for (int j = 0; j < array.length - 1 - i; j++) {
if (array[j] > array[j + 1]) {
// Swap elements
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}

// Display array method


public static void displayArray(int[] array) {
for (int num : array) {
System.out.print(num + " ");
}
System.out.println();
}
}

2. Time Complexity Table


Worst Average Best
Algorithm Notes
Case Case Case
Selection Always goes through all
O(n²) O(n²) O(n²)
Sort elements.
Best case is O(n) when
Bubble Sort O(n²) O(n²) O(n)
already sorted.
O(n log Most efficient for larger
Merge Sort O(n log n) O(n log n)
n) datasets.
Notes Summary:
 On average, Merge Sort performs the best due to its logarithmic splitting
and merging mechanism.
 Bubble Sort is faster for nearly sorted data because of its ability to detect
sorted arrays early.
 Selection Sort always has the same performance regardless of the data's
state.

3. Difference Between Linear and Binary Search


Linear Search:
 Searches sequentially from the beginning to the end of the array.
 Time Complexity: O(n).
 When to Use: Suitable for unsorted arrays or small datasets.
Binary Search:
 Requires a sorted array.
 Repeatedly divides the array into halves to locate the element.
 Time Complexity: O(log n).
 When to Use: Efficient for sorted datasets.
Example:
Search Type Steps to Find 50 in Array [10, 20, 30, 40, 50, 60, 70]
Linear 10 → 20 → 30 → 40 → 50 (5 comparisons).
Binary Check middle (40) → Check (50) (2 comparisons).

4. Sorting Order with Strings and Numbers


Strings:
 Sorted lexicographically (dictionary order).
 Example: "apple", "banana", "cherry" becomes "apple", "banana", "cherry".
 Sorting is case-sensitive, with uppercase letters appearing before lowercase.
Numbers:
 Sorted numerically in ascending or descending order depending on the
comparator.
 Example: [3, 1, 2] becomes [1, 2, 3] in ascending order.
Mixed Data:
 Sorting behavior is undefined for a mixed array of strings and numbers
because Java doesn’t allow direct comparisons between them. Attempting
to sort would result in a runtime error unless explicitly handled (e.g.,
converting everything to strings).

You might also like