Question

Write a program to sort an array of characters using INSERTION SORT. Note: You CAN NOT...

Write a program to sort an array of characters using INSERTION SORT.

Note: You CAN NOT use any built-in sorting function.

IN JAVA

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Scanner;

public class SortChar {

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter a charater ");
String string = sc.nextLine();
int j=0;
char temp=0;

char[] chars = string.toCharArray();//tocharArray not for sorting this for only read characters

for (int i = 0; i <chars.length; i++) {

      for ( j = 0; j < chars.length; j++) {

       if(chars[j]>chars[i]){
            temp=chars[i];
           chars[i]=chars[j];
           chars[j]=temp;
       }

   }

}

for(int k=0;k<chars.length;k++){
System.out.println(chars[k]);
}

}

}

Input:ahfgj

Output:afghj

Add a comment
Know the answer?
Add Answer to:
Write a program to sort an array of characters using INSERTION SORT. Note: You CAN NOT...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In C only Please! This lab is to write a program that will sort an array...

    In C only Please! This lab is to write a program that will sort an array of structs. Use the functions.h header file with your program. Create a source file named functions.c with the following: A sorting function named sortArray. It takes an array of MyStruct's and the length of that array. It returns nothing. You can use any of the sorting algorithms, you would like though it is recommended that you use bubble sort, insertion sort, or selection sort...

  • Data Structure using C++ only Write 4 different sorting functions: Selection Sort, Insertion Sort, Merge Sort...

    Data Structure using C++ only Write 4 different sorting functions: Selection Sort, Insertion Sort, Merge Sort and Quick sort. For each sort you may store the numbers in an array or a linked list (this may be different for each sort). Write your program so that it accepts arguments from the command line using argc and argv in the main function call.

  • C++ Write a program that can be used to compare Insertion Sort, Merge Sort and Quick...

    C++ Write a program that can be used to compare Insertion Sort, Merge Sort and Quick Sort. Program must: Read an array size from the user, dynamically an array of that size, and fill the array with random numbers Sort the array with the Insertion Sort, MergeSort and QuickSort algorithms studied in class, doing a time-stamp on each sort. Use your program to measure and record the time needed to sort random arrays of size 5000, 50000, and 500000. For...

  • This program should test the running time of these algorithms: Selection Sort Insertion Sort Bubble Sort...

    This program should test the running time of these algorithms: Selection Sort Insertion Sort Bubble Sort Merge Sort Quick Sort Heap Sort You have access to the implementation of all of these sorting algorithms, and you may use what is provided in your text directly. Be sure to cite the source of these implementations in the header of your program. Please maintain the property that these sorting algorithms sort arrays in ascending order. For this homework, you will write a...

  • I'm trying to do an insertion sort in which i sort the characters of a string,...

    I'm trying to do an insertion sort in which i sort the characters of a string, what do i replace inputString.charAt(j) with? note: //arr[j] = arr[j-1] is an example that my professor used for an array. (just a reminder for myself) Written in Java lang public static String sort(String inputString) if (inputS tring null) t //insertion sortsort characters for(int i = 0; i inputstring.length(); i++) int index - inputString.charAt(i); int j- i; while( i> 0 && inputString.charAt (i-1) > index)...

  • Write a Java program that implements Insertion Sort and sorts the following Array reverse alphabetically, printing...

    Write a Java program that implements Insertion Sort and sorts the following Array reverse alphabetically, printing the results to the console. {"five", "three", "eight","one","two","four","nine","seven","six","ten"}

  • In Java 2. Array Exercise ( 10 points) Write a Java program that will prompt the...

    In Java 2. Array Exercise ( 10 points) Write a Java program that will prompt the user to input a size of an array. Create an array of type int. Ask a user to fill the array. Create a functions sortArray() and mostFrequency(). The sortArray() function will sort number into incrementing order. The sorting function must be implemented from scratch and it must not use any function from the library. Feel free to use any sorting algorithm. The program will...

  • Write a java program to sort arrays using 3 different methods: Bubble Sort, Selection Sort and...

    Write a java program to sort arrays using 3 different methods: Bubble Sort, Selection Sort and Insertion Sort. The numbers to be sorted will be obtained using a library function which generates pseudo-random numbers. TO Do 1. Fill an array with 140 real numbers between 0.00 and 945.00. Generate the numbers using the subroutine that generates random numbers. Make a spare copy of the array; you will need it later. 2. Call a subroutine to print the contents of the...

  • USING C++ write a program that creates a modified version of Merge sort in which insertion...

    USING C++ write a program that creates a modified version of Merge sort in which insertion sort will be used for merging array elements.

  • Using C++, sort an array of 10,000 elements using the quick sort algorithm as follows: a....

    Using C++, sort an array of 10,000 elements using the quick sort algorithm as follows: a. Sort the array using pivot as the middle element of the array. b. Sort the array using pivot as the median of the first, last, and middle elements of the array. c. Sort the array using pivot as the middle element of the array. However, when the size of any sublist reduces to less than 20, sort thesublis t using an insertion sort. d....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT