Created
April 7, 2023 17:02
-
-
Save mdibrahimk48/02d8ed76145483cbb63c29effdd07110 to your computer and use it in GitHub Desktop.
This program takes a user input string, sorts it using the selection sort algorithm, and displays the sorted string and the count of each character in the string.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta http-equiv="X-UV-Compatible" content="IE=edge"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Sorting string using selection sort algorithm</title> | |
<!-- Made by Md. Ibrahim Khalil --> | |
</head> | |
<body> | |
<script> | |
var input = prompt("Please Enter Your Name: "); | |
var nameUpperCaseArray = input.split("").join("").toUpperCase().split(""); | |
function swap (arr,xp,yp) | |
{ | |
var temp =arr [xp]; | |
arr[xp] =arr[yp] | |
arr[yp] =temp; | |
} | |
function selecionSort(arr) | |
{ | |
var i,j, min_idx, n=arr.length; | |
for (i=0; i<n-1; i++) | |
{ | |
min_idx = i; | |
for(j=i+1; j<n; j++) | |
if (arr[j] < arr[min_idx]) | |
min_idx = j; | |
swap(arr, min_idx, i); | |
} | |
} | |
function getCountChar(arr) { | |
return arr.reduce((acc, cur) => { | |
if (acc[cur]) { | |
acc[cur] += 1; | |
return acc; | |
} | |
return { | |
...acc, | |
[cur]: 1 | |
}; | |
}, {}); | |
} | |
function printObject (message, object) | |
{ | |
var arr = Object.keys(object) | |
document.write(message); | |
for (var i=0; i<arr.length; i++) { | |
document.write(arr[i] + " = " + object[arr[i]] + " "); | |
} | |
document.write(" <br>"); | |
} | |
function printArry(message,arr) | |
{ | |
document.write(message); | |
for (var i=0; i <arr.length; i++){ | |
document.write(arr[i]+ " "); | |
} | |
document.write(" <br>"); | |
} | |
printArry("Your name (Input): ", nameUpperCaseArray); | |
//Output | |
selecionSort(nameUpperCaseArray); | |
document.write(" <br>"); | |
printArry("Name after sorting (Output): ", nameUpperCaseArray); | |
document.write(" <br>"); | |
printObject("Char count (Output): ", getCountChar(nameUpperCaseArray)); | |
document.write(" <br>"); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment