Home Answers Viewqa Java-Beginners Java insertion sort question

 
 


David Shipman
Java insertion sort question
1 Answer(s)      5 months and 12 days ago
Posted in : Java Beginners

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 Pages:
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
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 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 - 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 In Java
Insertion Sort In Java      ... to sort integer values of an array using insertion sort. Insertion sorting algorithm is similar to bubble sort. But insertion sort is more 
insertion sort
insertion sort  how many arrays needed for insertion sort and why
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? Assume that you have 5
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
complete this code (insertion sort) - Java Beginners
complete this code (insertion sort)  Your task is to develop part... to use a modiŻed version of insertion-sort algorithm which works as follows... to sort */ Scanner sc; /* for reading user input */ String line
Insertion Sort Applet
Insertion Sort Applet  I need Insertion Sort Applet code was design by Dr. Daniel Liang Please
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 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.... Example of Heap Sort in Java: public class eap_Sort{ public static void main
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.... Example of Merge Sort in Java public class mergeSort{ public static void main
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. Example of Quick Sort in Java: public class QuickSort
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
Question  Sir, give me a Java program to read data from keyboard up to names , sort in alphabetical order using bubble sort
Question
Question  Sir, please help me to write a JAVA program to read data from keyboard up to names and sort in alphabetical order using bubble sort
Help With Costructing Selection sort?
figure out just dont know how to incorporate the above question. Thanks in advance!   Please visit the following link: Java Selection Sort...Help With Costructing Selection sort?  Using a selection sort
question
question  <%@ page language="java" contentType="text/html; charset... java.util.Date; public class employeeInformation { int insertion; JFrame f; JPanel...(); s.applyPattern("dd/mm/yyyy"); //insertion l1=new JLabel("Employee ID
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
bubble sort
bubble sort  write a program in java using bubble sort
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
buble sort
buble sort  ascending order by using Bubble sort programm   Java BubbleSort Example
SEARCH AND SORT
SEARCH AND SORT  Cam any one provide me the code in java that : Program to search for MAX,MIN and then SORT the set using any of the Divide and conquer method
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
insertion in SQL - SQL
insertion in SQL  Query is "insert into employee values('"+eno...){ e.printStackTrace(); } } } Thanks  Hi, In java also we... is John''s Peter.... So in your java class it self before executing the query
insertion error - JSP-Servlet
insertion error  my first jsp page : In this i m getting all the values through a method called getAllDetails,the values are getting inserted... into table. below is the codeof jsp and java pages; function
Java: Example - String sort
Java: Example - String sort Sorting is a mechanism in which we sort the data in some order. There are so many sorting algorithm are present to sort the string. The example given below is based on Selection Sort. The Selection sort
question
question  how to put every data base connections (selection,insertion ,updation and so on) in one jsp page and how to import that in other pages of one project
question
question   sir plz tell me what should i give in title box. just i want java program for the question typed in this area
question
question   sir plz tell me what should i give in title box. just i want java program for the question typed in this area
Question
Question   When there is an exception in my program how java runtime system handles
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
question
question  dear sir/madam my question is how to compare two text format in java..we are java beginners..so we need the complete source code for above mentioned question...we have to compare each and every word
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
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
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
sort function - JSP-Servlet
sort function  How to sort a string variable in java   Hi friend, Please give in details and full source code to solve the problem. For information on java visit to : http://www.roseindia.net/java
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

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.