answersLogoWhite

0

What is sorted and unsorted?

Updated: 12/23/2022
User Avatar

Wiki User

11y ago

Want this question answered?

Be notified when an answer is posted

Add your answer:

Earn +20 pts
Q: What is sorted and unsorted?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Write a c program to sort an unsorted stack?

A stack is implicitly sorted by hierarchical nested order. It does not make sense to sort a stack. Do you mean a list? If so, please ask the question again.


How can you make a turbo c program by using one for loop to sort 3 numbers in ascending order?

Use the insertion sort algorithm. Insertion sort works on the premise that a set of 1 can always be regarded as a sorted set, thus in a set of n elements, we can divide the set in two, with a sorted set at the beginning (with 1 element) and an unsorted set at the end (with all other elements). We then take the first element of the unsorted set and insert it into the sorted set, repeating until the unsorted set is empty. We take the first element out of the unsorted set by copying it to a temporary variable, thus creating a gap in its place. We then examine the element to the left of the gap and, if the value is greater than the temporary, we copy it into the gap, effectively moving the gap one place to the left. We stop moving the gap when we reach the start of the array or the element to the left of the gap is not greater than the temporary. We then copy the temporary into the gap, increasing the sorted set by one element and reducing the unsorted set by one element. We can implement this algorithm using just one for loop: void sort (int* a, unsigned s) { // assume a refers to an array of length s for (int i=2; i<s; ++i) { int t = a[i]; // temporarily store a[i] outside the array int g = i; // index g is the "gap" in the array while (0<g && t<a[g-1]) { a[g]=a[g-1]; --g; } // move the gap to the correct insertion point a[g] = t; // insert the temporary } } Thus, to sort 3 numbers: int a[3] = {2, 3, 1}; // unsorted sort (a, 3); assert (a[0]==1); assert (a[1]==2); assert (a[2]==3);


How do you explain and illustrate the insertion sort algorithm to sort a list of n numbers?

The insertion sort algorithm works by dividing a set into two subsets, where the left set is the sorted set and the right set is the unsorted set. Initially, the sorted set has just one element because a set of one can always be regarded as being sorted. The idea is that we take the first element of the unsorted set and place it at the end of the sorted set. We then repeatedly swap it with the value to its immediate left every time that value is larger than the one we're inserting. When it is not larger, the new value has found its place and we move onto the next value. We repeat the process until the unsorted set is empty.The algorithm can be implemented most efficiently using the following pseudocode:procedure insertion_sort (A, n) is input: an array reference, A, and the count of its elements, nfor i ← 1 to n-1 inclusive j ← it ← A[i]while j > 0 and A[j-1] > tA[j] ← A[j-1]j ← j - 1end whileA[j] ← tend forThe outer for loop (i) traverses forwards through the unsorted set. On each iteration of i, the first element of the unsorted set, A[j], is moved to a temporary variable, t. A[j] now marks the insertion point which is initially at the end of the sorted set. The inner while loop (j) then traverses backwards through the sorted set to find the correct insertion point. The inner loop exits when the insertion point is at the start of the array (index 0) or the value to the left of the insertion point is not greater than the temporary value. If it is greater, we move that value into the insertion point and move the insertion point one place to the left. We then begin another iteration of the inner loop. When we exit the inner loop, we move our temporary value into the insertion point and begin another iteration of the outer loop, repeating until there are no more elements in the unsorted set.You will note that we never actually perform any swaps during the inner loop. This is because a swap incurs three separate move operations. By copying the value to a temporary and moving it back when the insertion point is found, we only need one move operation in the inner loop. If the insertion point only moves one position to the left then it's effectively the same as a swap, but each subsequent move will save us two move operations which improves efficiency overall.If the insertion point happens to be at the end of the sorted set then we incur two move operations that were never actually needed (we didn't need the temporary), however this is a relatively rare case and it would actually cost more to test for that one case than it would to simply move the value twice.


What is the best sorting technique for already sorted array?

If it is already sorted, the best is to leave the array as it is.If it is already sorted, the best is to leave the array as it is.If it is already sorted, the best is to leave the array as it is.If it is already sorted, the best is to leave the array as it is.


Can you use a sequential search on an unsorted array?

Sequential search is the only way to search an unsorted array unless you resort to a multi-threaded parallel search where all threads concurrently search a portion of the array sequentially.

Related questions

Do kettle lakes contain sorted or unsorted sediments?

sorted


Is an outwash plain sorted or unsorted?

outwash is sorted because it is running water


What is one interesting fact about conglomerate rocks?

The clasts in conglomerate can be sorted, partially sorted, or unsorted.


What are unsorted sediments?

\sediments that are in a body of water that are not sorted by their type of sediment. for example, if there were cobbles, pebbles, and silt, in a lake, that would be unsorted


What is ordering in DBMS?

changing of unsorted list to sorted list in a ordering from alphabets, numbers, ASC/DESC.


What is the difference between sediments in the outwash and sediments in the moraines?

Sediments that are in outwash are sorted sediments, organized by size, while sediments that are in moraine are unsorted.


How can a glacier deposit both unsorted and sorted materials?

it all comes together.


What is the code for sorting in C plus plus?

There are many sorting algorithms. One of the simplest to implement is the insertion sort. The following template function will sort an array of any type T that supports the less-than operator. It works by splitting the array into two subsets, where the left portion is sorted and the right is unsorted. Initially, the sorted portion has just one element since a set of one element can always be regarded as being sorted. We then work our way through each of the unsorted elements, from left to right, inserting them into their correct place in the sorted portion. To do this we need to store the current unsorted value thus creating a gap at the beginning of the unsorted portion. This gap then becomes the last element of the sorted portion, reducing the unsorted portion by one element. We then work our way through the sorted elements starting with the element to the left of the gap. If the stored value is less than the current element's value then we copy that element into the gap, thus moving the gap one position to the left. We continue in this manner until gap is at index 0 or the stored value is not less than the element to the left of the gap. We then place the stored value in the gap. We repeat this for all unsorted elements until there are none left, at which point the array is completely sorted. template<typename T> void sort(std::vector<T>& v) { if( v.size()>1 ) { for( size_t i=1; i<v.size(); ++i ) { T t = v[i]; size_t gap=i; while( gap && t<v[gap-1] ) v[gap]=v[gap--]; v[gap]=t; } } }


Is there any precondition to apply binary search procedure?

Sure. You have to have direct access to the sorted elements. You cannot use it if the elements are unsorted, or you cannot access them directly (magnetic tape for example).


What are the drawbacks of the binary search?

The only drawback I know of is that binary search requires that the list already be sorted. So if you have a really large unsorted list than binary search would not be the best option.


Write a c program to sort an unsorted stack?

A stack is implicitly sorted by hierarchical nested order. It does not make sense to sort a stack. Do you mean a list? If so, please ask the question again.


What will be the most probable arrangement of rock particles deposited directly by a glacier?

unsorted and not layered :)