Sorting String Values Without Using Any Method/Function

Sort string without any builtin methods

Your code is not applying any sort algorithm logic, I recommend you to read atleast 1 to solve your problem.

Below is the program, which produces the expected output from your program using selection sort.

swap and replace functions works fine.

function sort(str) {
var sorted = str;
//Selection sort
for (var i = 0; i < str.length; i++) {
for(var j = i + 1; j < str.length - 1; j++) {
if (str[i] < str[j]) {
str = swap(str, i, j)
}
}
}
return str;
}

console.log(sort("zaasfweqrouoicxzvjlmmknkniqwerpopzxcvdfaa"));
//output: aaaaccdeeffiijkklmmnnoooppqqrrsuvvwwxxzzz

How can i sort a string without using sort() method in java?

Looks like you need to sort the characters, so I'd start with

String input = "computer";
char[] characters = input.toCharArray();
//now sort characters using some algorithm
String output = new String(sorted_characters); //sorted_characters might be characters after sorting, if you sort in place

How to Sort an Array of Numbers of String Datatype without using Inbuilt sort() and atoi method in JavaScript

You seem to be approaching the problem by using a BubbleSort, so tried to come up with a solution using the same algorithm.

The issue is with your comparison.

You will see that

"1" < "10" === true

But

"2" < "10" === false

So you need to check each character of the string to determine whether the number is actually smaller or not.
Here is the code:

    const arr = ["1", "2", "10", "3", "21", "15"];
const len = arr.length;

const isGreater = (num1, num2) => {
if (num1.length < num2.length) return false;
for (let i = 0; i < len; ++i) {
if(num1[i] === num2[i]) continue;
return (num1[i] > num2[i]);
}
return false;
}

for (let i = 0; i < len; ++i) {
for (let j = 0; j < len - i - 1; ++j) {
if (arr[j].length > arr[j + 1].length || isGreater(arr[j], arr[j + 1])) {
let tmp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tmp;
}
}
}

console.log(arr);

The function isGreater will do that check for you.

Sort Multidimensional String Array Without Using Array.sort

You can do like this,:

 for(int i=0;i<numContacts;i++) {
for(int j=i+1;j<numContacts;j++) {
if(contactsArray [i][1].compareTo(contactsArray [j][1])>0) {
String[] temp = contactsArray [i];
contactsArray [i] = contactsArray [j];
contactsArray [j] = temp;
}
}
}

Sort string with integers and words without any change in their positions

numpy allows to write it more concisely, though doesn't eliminate the need for two separate sorts:

$ python3
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> from numpy.core.defchararray import isdecimal, lower
>>>
>>> s = "12 I have car 8 200 a"
>>>
>>> a = np.array(s.split())
>>>
>>> integer_mask = isdecimal(a)
>>> string_mask = ~integer_mask
>>> strings = a[string_mask]
>>>
>>> a[integer_mask] = np.sort(np.int_(a[integer_mask]))
>>> a[string_mask] = strings[np.argsort(lower(strings))]
>>>
>>> ' '.join(a)
'8 a car have 12 200 I'

Sorting array with numbers without sort() method

Here is a Bubble sort function for you to reference, but as mentioned there are many different sorting algorithms.

function bubbleSort(array) {  var done = false;  while (!done) {    done = true;    for (var i = 1; i < array.length; i += 1) {      if (array[i - 1] > array[i]) {        done = false;        var tmp = array[i - 1];        array[i - 1] = array[i];        array[i] = tmp;      }    }  }
return array;}
var numbers = [12, 10, 15, 11, 14, 13, 16];bubbleSort(numbers);console.log(numbers);

python: order a list of numbers without built-in sort, min, max function

I guess you are trying to do something like this:

data_list = [-5, -23, 5, 0, 23, -6, 23, 67]
new_list = []

while data_list:
minimum = data_list[0] # arbitrary number in list
for x in data_list:
if x < minimum:
minimum = x
new_list.append(minimum)
data_list.remove(minimum)

print (new_list)

#Added parenthesis



Related Topics



Leave a reply



Submit