Wet NAT M3 My Comme le TO SITTEN Und Bann Aar I Pot mach In this problem, we want to sort an inpet array. Problem 24 (ea
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am
Wet NAT M3 My Comme le TO SITTEN Und Bann Aar I Pot mach In this problem, we want to sort an inpet array. Problem 24 (ea
Problem 2A (easier) In this problem, develop an algorithm that, given an unsorted array of integers, creates a new array which is the sorted version of the given array. Here is the main idea of the algorithm: first scan through the original input array, finding the smallest element, and then insert this element into the beginning (index 0) of the new array, and remove this smallest element from the input array. Next, scan the (shortened) input array again, finding the smallest element, and then insert it into the next available position of the new array and remove it from the input array. Repeat the process until the original array becomes size 0 (this is also the time when the new array contains all the values in the original input array). Develop flowchart for this algorithm . o Give pre-condition and post-condition for your algorithm. o Hint: you will need nested loops for doing the work in one main algorithm. You can also write a sub- program to do some work. In this case the main algorithm has one loop. o Hint: to find the smallest element in an array, define a variable, say, minIndex, which is initially 0, then if an element at index k of the array is smaller than the element at index minIndex, update minIndex to be k. o For removing an element at index j from array arr, you can just say "remove element at index j from arr", or "remove arr[j] from arr" Start your flowchart as shown in the following picture, which first get an input array arr, and then D create new array newArr. o Save the flowchart as img_sortA.jpg *DE start arri newArr 3
n thi WC -C u v to fin an e ex t nov "re our 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 function problem2A() { var p = document.getElementById("mydata"); var myArray = []; var arrayMax = 10; var limit= 100; for (var i = 0; i < arrayMax; i++) myArray.push(Math.floor(Math.random()*limit)); p.innerHTML = myArray; /* display input array */ var newArray=[]; /* you implementation here */