Java insertion sort question

Java insertion sort question

I've got another program that I need help with. I am trying to write a Java method that accepts an array of strings, and sorts the strings using the insertion sort algorithm. Then I need to write a main method to create a string array with 5 city names (e.g. Atlanta, New York, Dallas, Omaha, San Francisco), and display the results in alphabetical order. I've some small experience in sorting int arrays, but I'm drawing a blank with regards to String arrays.

View Answers

December 8, 2012 at 4:50 PM

Here is a code that sorts string array with Insertion Sort.

public class SortStringArrayUsingInsertionSort{

public static void main(String[] args){
        String[] arr ={"Atlanta","New York","Dallas","Omaha","San Francisco"};
        int count = 0;
        String sortedArray[] = sort_sub(arr, arr.length); 
        for(int i=0;i<sortedArray.length;i++){
            System.out.println(sortedArray[i]);
          }
        }

    public static String[] sort_sub(String array[], int f){
        String temp="";
        for(int i=0;i<f;i++){
            for(int j=i+1;j<f;j++){
               if(array[i].compareToIgnoreCase(array[j])>0){
                   temp = array[i];
                   array[i]=array[j];
                   array[j]=temp;
               }
            }
        }
        return array;
    }
}









Related Tutorials/Questions & Answers:
Java insertion sort question
Java insertion sort question  I've got another program that I need help with. I am trying to write a Java method that accepts an array of strings, and sorts the strings using the insertion sort algorithm. Then I need to write
insertion sort
insertion sort  write a program in java using insertion sort
Advertisements
insertion sort
insertion sort  write a program in java using insertion sort
insertion sort
insertion sort  write a program in java using insertion sort
insertion sort
insertion sort  write a program in java using insertion sort
Insertion Sort - Java Beginners
Insertion Sort  Hello rose india java experts.If you don't mind.Can you help me.What is the code for Insertion Sort and Selection Sort that displays...: public class InsertionSort { public static void sort(String[] array) { int
insertion sort
insertion sort  how many arrays needed for insertion sort and why
Insertion Sort In Java
Insertion Sort In Java     ... In this example we are going to sort integer values of an array using insertion sort. Insertion sorting algorithm is similar to bubble sort. But insertion sort
Insertion Sort Java
Insertion Sort in Java is an algorithm that is used to sort integer values..., Insertion Sort in Java is less efficient when it comes to larger data sets... decreasing. How does Insertion Sort works in Java?ADS_TO_REPLACE_1 Assume
Insertion Sort Timer
Insertion Sort Timer  Welcome all I wanna program in java find the timer of insertion sort and find time complexity for random value thanks all
Insertion Sort Applet
Insertion Sort Applet  Please All , I need Insertion sort applet program
insertion sort applet code
insertion sort applet code  i need Insertion Sort Applet Program
Insertion Sort Applet
Insertion Sort Applet  I need Insertion Sort Applet code was design by Dr. Daniel Liang Please
complete this code (insertion sort) - Java Beginners
complete this code (insertion sort)  Your task is to develop part... to use a modied version of insertion-sort algorithm which works as follows... to sort */ Scanner sc; /* for reading user input */ String line
Java insertion sort with string array
Java insertion sort with string array In this tutorial, you will learn how to sort array of strings using string array with Insertion Sort. For this, we... array using inserting sort algorithm. In the main method, we have  invoked
Insertion Sort Problem
Insertion Sort Problem  So I have this in a class file. It is supposed to be an insertion sorter: int min, index=0, temp; for(int i=0;i<sorted.length;i++){ min=sorted[i]; for(int j=i+1;j<
Heap Sort in Java
Heap Sort in Java is used to sort integer values of an array. Like quicksort, insertion sort, bubble sort and other sorting methods, heap sort is used... values in heap is not 0. Example of Heap Sort in Java: public class eap
Merge Sort Java
Merge Sort in Java is used to sort integer values of an array. There are many methods to sort Java like bubble sort, insertion sort, selection sort, etc... sorted list remaining. Example of Merge Sort in Java public class
Quick Sort in Java
Quick sort in Java is used to sort integer values of an array... in comparison to other sorting algorithms like bubble sort, insertion sort, heap... into a sorted array. ADS_TO_REPLACE_1 Example of Quick Sort in Java: public class
please please i wanna insertion sort program find calendar to array value (1000,5000,10000), please help me
Insertion sort program find calendar to array value 1000,5000,10000  program find calendar for insertion sort in jfram to array 1000
Quick Sort in Java
Quick Sort in Java Quick Sort in Java is used to sort elements of an array..., insertion sort, heap sort and other sorting algorithms. First it divides an array... Sort algorithm in Java works? Quick Sort algorithm works on comparison sort
Java Dictionary-Sort,Extract
Java Dictionary-Sort,Extract  *I need to make an English(other language) dictionary by collecting english/other language words in a text file;then using that text file I need to sort the words collected in alphabetical order
search and sort techniques in java
search and sort techniques in java  Hi i attened an interview recently... they asked to write all searching and sorting technique codes in java.. i... of all these searchings and sortings in java...please help... Regards, Anugnya
bubble sort - Java Beginners
bubble sort  how to write program The bubble-sort algorithm in double...  Hi friend, Bubble Sort program : public class...[] = {10,5,3,89,110,120,1,8,2,12}; System.out.println("Values Before the sort:\n
sort java - Java Beginners
sort java  1. A statistics company wants to keep information of families. The information of a family is the family name, the number of members and first name of each member. The families are sorted alphabetically by family name
java question :)
java question :)  write java program to use vector in ArrayList with add, remove,sort   import java.util.Vector; public class... Element MUAYAD"); vc.add("Vector Element JAVA"); vc.add("Vector Element
array sort - Java Beginners
array sort  hi all, can anybody tell me how to sort an array without any in-built methods.   Hi Friend, Try the following code: public class SortArrayWithoutUsingMethod{ public static void sortArray(int
heap sort in java
heap sort in java  plz modify this program so that it can take input as integers and string both..... public class heap_Sort{ public static void...("\n Heap Sort\n---------------\n"); System.out.println("\n Unsorted Array\n\n
Sort
Sort  program to sort a list of numbers in decendimg order   Hi Friend, Try the following code:ADS_TO_REPLACE_1 import java.util.*; class SortListInDescendingOrder{ public static void main(String[] args
Sort
with this A program is required to ask users to rate the Java programming language... Scanner(System.in); System.out.print("Rate Java(0-10): "); int rate..."); } } } } System.out.print("Invalid! Rate Java within the range(0-10): "); rate=input.nextInt
Question in Java ??
Question in Java ??  Welcome every One ,I have Q in Java : Write aprogram that print the falewing table using SQRT method in the Math Class? Number 0 ,2, . . . ,18,20 squrfoot 00000 1.4142 5.2426 5.4721
Question in Java ??
Question in Java ??   Welcome every One ,I have Q in Java : Write aprogram that print the falewing table using SQRT method in the Math Class? Number squrfoot
Question in Java ??
Question in Java ??  Welcome every One ,I have Q in Java : Write aprogram that print the falewing table using SQRT method in the Math Class? Number 0 ,2, . . . ,18,20 squrfoot 00000 1.4142 5.2426 5.4721
Selection Sort in Java
Selection sort in Java is used to sort the unsorted values in an array... the selection sort in Java. In selection sort algorithm, first assign minimum index... Sort in Java: public class selectionSort{ public static void main(String
Merge Sort String Array in Java
Merge Sort String Array in Java  Hello, I am trying to implement a merge sort algorithm that sorts an array of Strings. I have seen numerous.... Note: I have a separate class for merge sort in which i pass my list and the size
How to sort ArrayList in java
How to sort ArrayList in java In this section you will learn how to sort ArrayList in java. ArrayList support dynamic array that can grow as needed. Array... which sort the Arraylist. Now here is the code to sort the ArrayList in java
Question on java
Question on java  why java is not purely oop?   java is not purely OOP because 1.) Multiple Inheritance is not supported.ADS_TO_REPLACE_1 2.) It uses primitive data type. 3.) static method can be called without
Question Java>?
Question Java>?  **ï??· Create a new class called Bank and in the main method use class Account (lab 4) to create an array of 10 accounts. ï??· Enter data for all accounts (account number,name, balance). ï??· Add
Bubble Sort in Java
Bubble Sort aka exchange sort in Java is used to sort integer values..._TO_REPLACE_1 Example of Bubble Sort in Java: public class BubbleSort { public static... Sort compares first pair of adjacent elements and put larger value at higher
java question
java question   Create a class called Complex to perform arithmetic operations with complex numbers. 1- Use double variables to represent the fields of the class. 2- Provide a no-argument constructor with default values in case
Use a tree stack to sort number in java?
Use a tree stack to sort number in java?  The Question is : Three stacks can be used to sort a list of numbers. Assuming stack in holds the input list of numbers, stack out is to hold the output list after sorting the numbers
Use a tree stack to sort number in java?
Use a tree stack to sort number in java?  The Question is : Three stacks can be used to sort a list of numbers. Assuming stack in holds the input list of numbers, stack out is to hold the output list after sorting the numbers
Use a tree stack to sort number in java?
Use a tree stack to sort number in java?  The Question is : Three stacks can be used to sort a list of numbers. Assuming stack in holds the input list of numbers, stack out is to hold the output list after sorting the numbers
Use a tree stack to sort number in java?
Use a tree stack to sort number in java?  The Question is : Three stacks can be used to sort a list of numbers. Assuming stack in holds the input list of numbers, stack out is to hold the output list after sorting the numbers
Use a tree stack to sort number in java?
Use a tree stack to sort number in java?  The Question is : Three stacks can be used to sort a list of numbers. Assuming stack in holds the input list of numbers, stack out is to hold the output list after sorting the numbers
Use a tree stack to sort number in java?
Use a tree stack to sort number in java?  The Question is : Three stacks can be used to sort a list of numbers. Assuming stack in holds the input list of numbers, stack out is to hold the output list after sorting the numbers
Question
Question  Sir, give me a Java program to read data from keyboard up to names , sort in alphabetical order using bubble sort
java question
java question  the purpose of defining multiple constructor in java
java question
java question  find the 127 twin pair in java
JAVA QUESTION
JAVA QUESTION  How to view image on Frame in swing(or)awt in Java

Ads