-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path46. Sort array of strings alphabetically .js
More file actions
129 lines (110 loc) · 4.12 KB
/
46. Sort array of strings alphabetically .js
File metadata and controls
129 lines (110 loc) · 4.12 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// sort array of strings alphabetically :
// --------------------------------------------------------------------------------------------------------------::
const words = ["banana", "apple", "cherry", "date"];
// Copy to new array
const sorted = [];
for (let i = 0; i < words.length; i++) {
sorted[i] = words[i];
}
// Keep swapping until sorted
let swapped = true;
while (swapped) {
swapped = false;
for (let i = 0; i < sorted.length - 1; i++) {
if (sorted[i] > sorted[i + 1]) {
// Swap
let temp = sorted[i];
sorted[i] = sorted[i + 1];
sorted[i + 1] = temp;
swapped = true;
}
}
}
console.log("Original:", words);
console.log("Sorted:", sorted);
/*
EXPLANATION:
1. Copy array to avoid changing original
2. Compare each pair of words
3. If left word > right word alphabetically, swap them
4. Keep doing this until no more swaps needed
5. Done! Array is now sorted alphabetically
*/
// SUPER MANUAL (NO BUILT-IN FEATURES) - ALPHABETICAL
// ------------------------------------------------------------------------------------
let arraySize = 0;
const testArray = ["dog", "cat", "bird", "ant"];
// Calculate array size manually (no .length property)
while (testArray[arraySize] !== undefined) {
arraySize = arraySize + 1;
}
// Create result array manually
const manualResult = new Array();
let copyIndex = 0;
// Copy array elements manually (no built-in methods)
while (testArray[copyIndex] !== undefined) {
manualResult[copyIndex] = testArray[copyIndex];
copyIndex = copyIndex + 1;
}
// Manual sorting algorithm for alphabetical order
let stillChanging = true;
while (stillChanging) {
stillChanging = false; // Assume no changes needed
let checkIndex = 0;
// Compare adjacent elements manually
while (checkIndex < arraySize - 1) {
// If left string comes AFTER right string alphabetically, swap them
if (manualResult[checkIndex] > manualResult[checkIndex + 1]) {
// Manual swap without using convenient variable names
let temp = manualResult[checkIndex];
manualResult[checkIndex] = manualResult[checkIndex + 1];
manualResult[checkIndex + 1] = temp;
stillChanging = true; // Mark that a change was made
}
checkIndex = checkIndex + 1; // Move to next pair
}
}
console.log("Super manual alphabetical:", manualResult);
/*
EXPLANATION:
- Even more manual: calculate array size ourselves (no .length)
- Count elements by checking when we hit undefined
- Copy and sort using only basic operations
- String comparison works same as numbers: "dog" > "cat" is true
- Shows how much work JavaScript does for us behind the scenes
- Same sorting logic but comparing strings instead of numbers
*/
// BUBBLE SORT FUNCTION (STRUCTURED APPROACH) - ALPHABETICAL
// ------------------------------------------------------------------------------------
function bubbleSortAlphabetical(arr) {
// Create a copy so we don't modify original array
const result = [];
for (let i = 0; i < arr.length; i++) {
result[i] = arr[i];
}
// Bubble sort algorithm for alphabetical order
for (let i = 0; i < result.length; i++) {
for (let j = 0; j < result.length - i - 1; j++) {
// Compare adjacent string elements
if (result[j] > result[j + 1]) { // ALPHABETICAL: swap if left > right
// Swap if they're in wrong order
let temp = result[j];
result[j] = result[j + 1];
result[j + 1] = temp;
}
}
}
return result;
}
const words1 = ["zebra", "apple", "dog", "cat", "banana"];
console.log("Given:", words1);
console.log("Bubble sort alphabetical:", bubbleSortAlphabetical(words1));
/*
EXPLANATION:
- Proper bubble sort with exact number of passes needed
- Outer loop controls passes, inner loop does comparisons
- Each pass puts alphabetically latest string in correct position
- More efficient than Section 1 (fixed number of passes)
- Function format makes it reusable for any string array
- Exact same code as number sorting - JavaScript handles string comparison!
*/