Click here to Skip to main content
15,889,881 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need to sort an array using recursive function.If it sortedthen it must return true, otherwise must return false.

What I have tried:

I have tried like this`

function sort(x, i=0){
	if(i == x.length){
		return x;
	}
		if(x[i-1] > x[i]){
        var tmp = x[i - 1];
        x[i - 1] = x[i];
        x[i] = tmp;
	}
	return sort(x, i+1);
}
console.log(sort([2,5,100,6,85,12,45]));
Posted
Updated 11-Mar-22 18:05pm
v2

If this is a genuine question, then use Array.prototype.sort() - JavaScript | MDN[^]

If this is a homework or interview question, then you'll need to do your own research. (You'll also want to start with a more random array of numbers, to see where your proposed algorithm falls over.) Wikipedia has a good list of the most popular sorting algorithms[^] to get you started.
 
Share this answer
 
Comments
Suren97 23-Jul-18 11:29am    
Please help me if you can, i'm not good at english and i can't understand when i read it
Richard Deeming 23-Jul-18 11:34am    
If this is homework, or an interview question, then you need to come up with the solution.

If it is NOT homework or an interview question, then use the built-in method:
console.log([2,5,100,6,85,12,45].sort(function(x,y){return x-y;}));
Suren97 23-Jul-18 11:37am    
It is a homework, can't you help me bro?
Richard Deeming 23-Jul-18 11:40am    
The Wikipedia link in my answer lists the most popular sorting algorithms, with links to more information.

What you've got so far looks like an incomplete version of the Bubble sort[^].
Suren97 23-Jul-18 11:40am    
I have changed my solution, you can look, but it returns wrong again :(
Quote:
I need to sort an array using recursive function.If it sorted then it must return true, otherwise must return false.

Just the statement makes no sense !
A sort function will return the sorted array, nothing else.
What ever the solution you choose, you need to select an algorithm and then translate to code. Recursive is not mandatory, it is your choice.

We do not do your HomeWork.
HomeWork problems are simplified versions of the kind of problems you will have to solve in real life, their purpose is learning and practicing. The kind of help you want is like trying to break an olympic record by having someone else trainning for you because you are a beginner.

HomeWork is not set to test your skills at begging other people to do your work, it is set to make you think and to help your teacher to check your understanding of the courses you have taken and also the problems you have at applying them.
Any failure of you will help your teacher spot your weaknesses and set remedial actions.
Any failure of you will help you to learn what works and what don't, it is called 'trial and error' learning.
So, give it a try, reread your lessons and start working. If you are stuck on a specific problem, show your code and explain this exact problem, we might help.
 
Share this answer
 
const arr = [2, 5, 100, 6, 85, 12, 45]
function sort(x, i = x.length - 1) {
let tmp
if (i == 0) {
return x;
}
for (let j = i; j >= 0; j--) {
if (x[j - 1] > x[i]) {
tmp = x[j - 1];
x[j - 1] = x[i];
x[i] = tmp;
}
}
return sort(x, i - 1);
}
const sorted_arr = sort(arr)
console.log(sorted_arr)
 
Share this answer
 
Comments
CHill60 12-Mar-22 4:16am    
Doing someone's homework for them doesn't help anyone. An un-formatted, un-commented code dump isn't a solution either.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900