-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
109 lines (87 loc) · 2.81 KB
/
Program.cs
File metadata and controls
109 lines (87 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
using System;
namespace CSharpAlgorithms
{
internal class Program
{
static void Main(string[] args)
{
while (true)
{
Console.WriteLine("\n===== MENU =====");
Console.WriteLine("1. Sort Exam Scores (Selection Sort)");
Console.WriteLine("2. Merge Strings Alternately");
Console.WriteLine("3. Exit");
Console.Write("Choose an option: ");
string choice = Console.ReadLine();
if (choice == "1")
{
RunSelectionSort();
}
else if (choice == "2")
{
RunMergeStrings();
}
else if (choice == "3")
{
break;
}
else
{
Console.WriteLine("Invalid choice.");
}
}
}
// PROBLEM 1: Selection Sort
static void RunSelectionSort()
{
Console.Write("\nEnter exam scores separated by spaces: ");
string input = Console.ReadLine();
int[] scores = Array.ConvertAll(
input.Split(' ', StringSplitOptions.RemoveEmptyEntries),
int.Parse
);
SelectionSort(scores);
Console.WriteLine("Sorted Scores:");
Console.WriteLine(string.Join(" ", scores));
}
static void SelectionSort(int[] arr)
{
for (int i = 0; i < arr.Length - 1; i++)
{
int minIndex = i;
for (int j = i + 1; j < arr.Length; j++)
{
if (arr[j] < arr[minIndex])
minIndex = j;
}
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}
// PROBLEM 2: Merge Strings Alternately
static void RunMergeStrings()
{
Console.Write("\nEnter word1: ");
string word1 = Console.ReadLine();
Console.Write("Enter word2: ");
string word2 = Console.ReadLine();
string merged = MergeAlternately(word1, word2);
Console.WriteLine("Merged String: " + merged);
}
static string MergeAlternately(string word1, string word2)
{
int i = 0;
int j = 0;
string result = "";
while (i < word1.Length || j < word2.Length)
{
if (i < word1.Length)
result += word1[i++];
if (j < word2.Length)
result += word2[j++];
}
return result;
}
}
}