Remove negative numbers and sort an array

The while loop never stops, that’s probably the reason your code freezes. Your code only changes the address when the if statement is true. Which the array in your main() will stuck on the second (a[1]) element. So if we change change the address when the if statement is false… #include<stdio.h> #include<stdlib.h> void swap(int *p, … Read more

arranging numbers in ascending order [closed]

This is quite basic program called selection sort. The wiki article is: Selection sort. Basically in this program, i is first pointing to the first element in the array. Then, j points to the next element. If, the element j is smaller than element i, they get swapped. After swapping, the inner for loop still … Read more

Sort out array of object using its value [duplicate]

Use String.localeCompare() with the numeric option to sort using possibility, but use – (minus) to get descending results. If the comparison returns 0 (equals), use localeCompare again to compare the names: const array = [{“name”:”fever”,”possibility”:”20%”},{“name”:”hiv”,”possibility”:”25%”},{“name”:”heart-attack”,”possibility”:”20%”},{“name”:”covid”,”possibility”:”40%”}] const result = array.sort((a, b) => -a.possibility.localeCompare(b.possibility, undefined, { numeric: true }) || a.name.localeCompare(b.name) ) console.log(result)

C++ Sorting Algorithms [closed]

Make one pass through the sequence to find the largest value, the second largest value, and the smallest value. Swap the largest to one end, the second largest to the other end, and the smallest to the middle. Voila: largest items are on the ends and the smallest is in the middle. Calling this a … Read more