Docsity

ASM1 - Data Structures and Algorithms, Exercises of Creative Thinking

Toggle sidebar
bg1
Page 1 of 5
Lab6 : Sorting in Java (Part 1)
Exercise 1: Implementing Bubble Sort
Objective:
Understand the working of the Bubble Sort algorithm.
Implement Bubble Sort in Java.
Instructions:
1. Create a class named BubbleSort.
2. Write a method bubbleSort(int[] arr) to implement the Bubble Sort
algorithm.
3. Use the array [64, 34, 25, 12, 22, 11, 90] as input.
4. Display the array after each iteration.
5. Count the total number of swaps performed.
Exercise 2: Implementing Insertion Sort
Objective:
Understand the working of the Insertion Sort algorithm.
Implement Insertion Sort in Java.
pf3
pf4
pf5

Partial preview of the text

Download ASM1 - Data Structures and Algorithms and more Exercises Creative Thinking in PDF only on Docsity!

Lab6 : Sorting in Java (Part 1)

Exercise 1: Implementing Bubble Sort

Objective:

 Understand the working of the Bubble Sort algorithm.  Implement Bubble Sort in Java.

Instructions:

  1. Create a class named BubbleSort.
  2. Write a method bubbleSort(int[] arr) to implement the Bubble Sort algorithm.
  3. Use the array [64, 34, 25, 12, 22, 11, 90] as input.
  4. Display the array after each iteration.
  5. Count the total number of swaps performed.

Exercise 2: Implementing Insertion Sort

Objective:

 Understand the working of the Insertion Sort algorithm.  Implement Insertion Sort in Java.

Instructions:

  1. Create a class named InsertionSort.
  2. Write a method insertionSort(int[] arr) to implement the Insertion Sort algorithm.
  3. Use the array [12, 11, 13, 5, 6] as input.
  4. Display the array state after each insertion. Exercise 3: Implementing Selection Sort

Objective:

 Understand how to find the smallest value and place it in the correct position.

Instructions:

  1. Create a class named SelectionSort.
  2. Write a method selectionSort(int[] arr) to implement the Selection Sort algorithm.
  3. Use the array [29, 10, 14, 37, 13].
  4. Display the array state after each swap.

Exercise 5: Checking Stability

Objective:

 Determine the stability of each sorting algorithm.

Instructions:

  1. Use an array of tuples (e.g., (4, 'a'), (2, 'b'), (2, 'c'), (8, 'd')).
  2. Sort the array using Bubble Sort, Insertion Sort, and Selection Sort.
  3. Verify whether the relative order of elements with the same value is preserved. Exercise 6: Real-World Application

Objective:

 Apply sorting algorithms to solve a real-world problem.

Instructions:

  1. Create a list of students: ("Alice", 85), ("Bob", 95), ("Charlie", 75), ("David", 90).
  2. Sort the students by their scores in ascending order using Insertion Sort.
  3. Display the sorted list of students.