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; } }

Ads

Related Tutorials/Questions & Answers:

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

Advertisements

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 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

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 I need Insertion Sort Applet code was design by Dr. Daniel Liang Please

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 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 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 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 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

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 is used to sort elements of an array..., insertion sort, heap sort and other sorting algorithms. First it divides an array... in Java works? Quick Sort algorithm works on comparison sort that means

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

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 *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

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 ?? 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 ?? 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

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

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

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

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