Class: DSAVisualizer::Algorithms::Sorting

Inherits:
Object
  • Object
show all
Defined in:
lib/dsa_visualizer/algorithms/sorting.rb

Class Method Summary collapse

Class Method Details

.demoObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/dsa_visualizer/algorithms/sorting.rb', line 16

def self.demo
  Visualizer.print_header("SORTING ALGORITHMS - Core Level Visualization")
  
  Visualizer.print_section("1. Bubble Sort")
  
  ruby_code = <<~RUBY
    def bubble_sort(arr)
      n = arr.length
      (n-1).times do |i|
        (n-i-1).times do |j|
          if arr[j] > arr[j+1]
            arr[j], arr[j+1] = arr[j+1], arr[j]
          end
        end
      end
      arr
    end
  RUBY

  cpp_code = <<~CPP
    void bubbleSort(int arr[], int n) {
      for(int i = 0; i < n-1; i++) {
        for(int j = 0; j < n-i-1; j++) {
          if(arr[j] > arr[j+1]) {
            std::swap(arr[j], arr[j+1]);
          }
        }
      }
    }
  CPP

  explanation = "Both implementations identical in logic. Ruby's swap is cleaner syntax. Time: O(n²), Space: O(1). Stable sort."
  
  Visualizer.print_comparison(ruby_code, cpp_code, explanation)
  
  arr = [64, 34, 25, 12, 22]
  puts "\nOriginal: #{arr.inspect}"
  
  # Simulate one pass
  puts "\nAfter first pass:"
  n = arr.length
  (n-1).times do |j|
    if arr[j] > arr[j+1]
      arr[j], arr[j+1] = arr[j+1], arr[j]
      puts "  Swapped #{arr[j+1]} and #{arr[j]}: #{arr.inspect}"
    end
  end
  
  Visualizer.print_section("2. Quick Sort")
  puts "\nDivide and conquer algorithm"
  puts "Average: O(n log n), Worst: O(n²)"
  puts "In-place, not stable"
  
  puts "\n\n🎯 Sorting Comparison:".colorize(:green).bold
  puts "  Bubble Sort: O(n²) - simple, slow"
  puts "  Quick Sort: O(n log n) - fast, in-place"
  puts "  Merge Sort: O(n log n) - stable, needs extra space"
  puts "  Heap Sort: O(n log n) - in-place, not stable"
end

.learn_bubbleObject



4
5
6
# File 'lib/dsa_visualizer/algorithms/sorting.rb', line 4

def self.learn_bubble
  demo
end

.learn_mergeObject



8
9
10
# File 'lib/dsa_visualizer/algorithms/sorting.rb', line 8

def self.learn_merge
  puts "\n📚 Coming soon: Merge Sort - Divide and Conquer"
end

.learn_quickObject



12
13
14
# File 'lib/dsa_visualizer/algorithms/sorting.rb', line 12

def self.learn_quick
  puts "\n📚 Coming soon: Quick Sort - Partition-based"
end