Java : Sort integer array without using Arrays.sort()
This is the instruction in one of the exercises in our Java class. Before anything else, I would like to say that I 'do my homework' and I'm not just being lazy asking someone on Stack Overflow to answer this for me. This specific item has been my problem out of all the other exercises because I've been struggling to find the 'perfect algorithm' for this.
Write JAVA program that will input 10 integer values and display either in ascending or descending order. Note: Arrays.sort() is not allowed.
This is the code I have come up with, it works but it has one obvious flaw. If I enter the same value twice or more, for example:
5, 5, 5, 4, 6, 7, 3, 2, 8, 10
Only one of the three 5s entered would be counted and included in the output. The output I get (for the ascending order) is:
2 3 4 5 0 0 6 7 8 10.
import java.util.Scanner;
public class Exer3AscDesc
{
public static void main(String args)
{
Scanner scan = new Scanner(System.in);
int tenNums=new int[10], orderedNums=new int[10];
int greater;
String choice;
//get input
System.out.println("Enter 10 integers : ");
for (int i=0;i<tenNums.length;i++)
{
System.out.print(i+1+"=> ");
tenNums[i] = scan.nextInt();
}
System.out.println();
//imperfect number ordering algorithm
for(int indexL=0;indexL<tenNums.length;indexL++)
{
greater=0;
for(int indexR=0;indexR<tenNums.length;indexR++)
{
if(tenNums[indexL]>tenNums[indexR])
{
greater++;
}
}
orderedNums[greater]=tenNums[indexL];
}
//ask if ascending or descending
System.out.print("Display order :nA - AscendingnD - DescendingnEnter your choice : ");
choice = scan.next();
//output the numbers based on choice
if(choice.equalsIgnoreCase("a"))
{
for(greater=0;greater<orderedNums.length;greater++)
{
System.out.print(orderedNums[greater]+" ");
}
}
else if(choice.equalsIgnoreCase("d"))
{
for(greater=9;greater>-1;greater--)
{
System.out.print(orderedNums[greater]+" ");
}
}
}
}
java arrays sorting integer int
add a comment |
This is the instruction in one of the exercises in our Java class. Before anything else, I would like to say that I 'do my homework' and I'm not just being lazy asking someone on Stack Overflow to answer this for me. This specific item has been my problem out of all the other exercises because I've been struggling to find the 'perfect algorithm' for this.
Write JAVA program that will input 10 integer values and display either in ascending or descending order. Note: Arrays.sort() is not allowed.
This is the code I have come up with, it works but it has one obvious flaw. If I enter the same value twice or more, for example:
5, 5, 5, 4, 6, 7, 3, 2, 8, 10
Only one of the three 5s entered would be counted and included in the output. The output I get (for the ascending order) is:
2 3 4 5 0 0 6 7 8 10.
import java.util.Scanner;
public class Exer3AscDesc
{
public static void main(String args)
{
Scanner scan = new Scanner(System.in);
int tenNums=new int[10], orderedNums=new int[10];
int greater;
String choice;
//get input
System.out.println("Enter 10 integers : ");
for (int i=0;i<tenNums.length;i++)
{
System.out.print(i+1+"=> ");
tenNums[i] = scan.nextInt();
}
System.out.println();
//imperfect number ordering algorithm
for(int indexL=0;indexL<tenNums.length;indexL++)
{
greater=0;
for(int indexR=0;indexR<tenNums.length;indexR++)
{
if(tenNums[indexL]>tenNums[indexR])
{
greater++;
}
}
orderedNums[greater]=tenNums[indexL];
}
//ask if ascending or descending
System.out.print("Display order :nA - AscendingnD - DescendingnEnter your choice : ");
choice = scan.next();
//output the numbers based on choice
if(choice.equalsIgnoreCase("a"))
{
for(greater=0;greater<orderedNums.length;greater++)
{
System.out.print(orderedNums[greater]+" ");
}
}
else if(choice.equalsIgnoreCase("d"))
{
for(greater=9;greater>-1;greater--)
{
System.out.print(orderedNums[greater]+" ");
}
}
}
}
java arrays sorting integer int
Would it be cheating to create a PriorityQueue, add all the items, and then repeatedly poll until the queue is empty?
– Jim Mischel
Aug 31 '16 at 11:39
add a comment |
This is the instruction in one of the exercises in our Java class. Before anything else, I would like to say that I 'do my homework' and I'm not just being lazy asking someone on Stack Overflow to answer this for me. This specific item has been my problem out of all the other exercises because I've been struggling to find the 'perfect algorithm' for this.
Write JAVA program that will input 10 integer values and display either in ascending or descending order. Note: Arrays.sort() is not allowed.
This is the code I have come up with, it works but it has one obvious flaw. If I enter the same value twice or more, for example:
5, 5, 5, 4, 6, 7, 3, 2, 8, 10
Only one of the three 5s entered would be counted and included in the output. The output I get (for the ascending order) is:
2 3 4 5 0 0 6 7 8 10.
import java.util.Scanner;
public class Exer3AscDesc
{
public static void main(String args)
{
Scanner scan = new Scanner(System.in);
int tenNums=new int[10], orderedNums=new int[10];
int greater;
String choice;
//get input
System.out.println("Enter 10 integers : ");
for (int i=0;i<tenNums.length;i++)
{
System.out.print(i+1+"=> ");
tenNums[i] = scan.nextInt();
}
System.out.println();
//imperfect number ordering algorithm
for(int indexL=0;indexL<tenNums.length;indexL++)
{
greater=0;
for(int indexR=0;indexR<tenNums.length;indexR++)
{
if(tenNums[indexL]>tenNums[indexR])
{
greater++;
}
}
orderedNums[greater]=tenNums[indexL];
}
//ask if ascending or descending
System.out.print("Display order :nA - AscendingnD - DescendingnEnter your choice : ");
choice = scan.next();
//output the numbers based on choice
if(choice.equalsIgnoreCase("a"))
{
for(greater=0;greater<orderedNums.length;greater++)
{
System.out.print(orderedNums[greater]+" ");
}
}
else if(choice.equalsIgnoreCase("d"))
{
for(greater=9;greater>-1;greater--)
{
System.out.print(orderedNums[greater]+" ");
}
}
}
}
java arrays sorting integer int
This is the instruction in one of the exercises in our Java class. Before anything else, I would like to say that I 'do my homework' and I'm not just being lazy asking someone on Stack Overflow to answer this for me. This specific item has been my problem out of all the other exercises because I've been struggling to find the 'perfect algorithm' for this.
Write JAVA program that will input 10 integer values and display either in ascending or descending order. Note: Arrays.sort() is not allowed.
This is the code I have come up with, it works but it has one obvious flaw. If I enter the same value twice or more, for example:
5, 5, 5, 4, 6, 7, 3, 2, 8, 10
Only one of the three 5s entered would be counted and included in the output. The output I get (for the ascending order) is:
2 3 4 5 0 0 6 7 8 10.
import java.util.Scanner;
public class Exer3AscDesc
{
public static void main(String args)
{
Scanner scan = new Scanner(System.in);
int tenNums=new int[10], orderedNums=new int[10];
int greater;
String choice;
//get input
System.out.println("Enter 10 integers : ");
for (int i=0;i<tenNums.length;i++)
{
System.out.print(i+1+"=> ");
tenNums[i] = scan.nextInt();
}
System.out.println();
//imperfect number ordering algorithm
for(int indexL=0;indexL<tenNums.length;indexL++)
{
greater=0;
for(int indexR=0;indexR<tenNums.length;indexR++)
{
if(tenNums[indexL]>tenNums[indexR])
{
greater++;
}
}
orderedNums[greater]=tenNums[indexL];
}
//ask if ascending or descending
System.out.print("Display order :nA - AscendingnD - DescendingnEnter your choice : ");
choice = scan.next();
//output the numbers based on choice
if(choice.equalsIgnoreCase("a"))
{
for(greater=0;greater<orderedNums.length;greater++)
{
System.out.print(orderedNums[greater]+" ");
}
}
else if(choice.equalsIgnoreCase("d"))
{
for(greater=9;greater>-1;greater--)
{
System.out.print(orderedNums[greater]+" ");
}
}
}
}
java arrays sorting integer int
java arrays sorting integer int
edited Feb 28 '15 at 19:27
gvlasov
6,581164984
6,581164984
asked Nov 25 '12 at 5:34
ransan32ransan32
80228
80228
Would it be cheating to create a PriorityQueue, add all the items, and then repeatedly poll until the queue is empty?
– Jim Mischel
Aug 31 '16 at 11:39
add a comment |
Would it be cheating to create a PriorityQueue, add all the items, and then repeatedly poll until the queue is empty?
– Jim Mischel
Aug 31 '16 at 11:39
Would it be cheating to create a PriorityQueue, add all the items, and then repeatedly poll until the queue is empty?
– Jim Mischel
Aug 31 '16 at 11:39
Would it be cheating to create a PriorityQueue, add all the items, and then repeatedly poll until the queue is empty?
– Jim Mischel
Aug 31 '16 at 11:39
add a comment |
12 Answers
12
active
oldest
votes
You can find so many different sorting algorithms in internet, but if you want to fix your own solution you can do following changes in your code:
Instead of:
orderedNums[greater]=tenNums[indexL];
you need to do this:
while (orderedNums[greater] == tenNums[indexL]) {
greater++;
}
orderedNums[greater] = tenNums[indexL];
This code basically checks if that particular index is occupied by a similar number, then it will try to find next free index.
Note: Since the default value in your sorted array elements is 0, you need to make sure 0 is not in your list. otherwise you need
to initiate your sorted array with an especial number that you sure is
not in your list e.g: Integer.MAX_VALUE
Thanks a lot mohammad, I spent too much time trying to formulate different algorithms, I didn't realize how simple it was that I just needed to add that one line to my code.
– ransan32
Nov 25 '12 at 15:53
add a comment |
Simple sorting algorithm Bubble sort:
public static void main(String args) {
int arr = new int { 6, 8, 7, 4, 312, 78, 54, 9, 12, 100, 89, 74 };
for (int i = 0; i < arr.length; i++) {
for (int j = i + 1; j < arr.length; j++) {
int tmp = 0;
if (arr[i] > arr[j]) {
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
}
}
add a comment |
Here is one simple solution
public static void main(String args) {
//Without using Arrays.sort function
int i;
int nos = {12,9,-4,-1,3,10,34,12,11};
System.out.print("Values before sorting: n");
for(i = 0; i < nos.length; i++)
System.out.println( nos[i]+" ");
sort(nos, nos.length);
System.out.print("Values after sorting: n");
for(i = 0; i <nos.length; i++){
System.out.println(nos[i]+" ");
}
}
private static void sort(int nos, int n) {
for (int i = 1; i < n; i++){
int j = i;
int B = nos[i];
while ((j > 0) && (nos[j-1] > B)){
nos[j] = nos[j-1];
j--;
}
nos[j] = B;
}
}
And the output is:
Values before sorting:
12
9
-4
-1
3
10
34
12
11
Values after sorting:
-4
-1
3
9
10
11
12
12
34
add a comment |
Simple way :
int a={6,2,5,1};
System.out.println(Arrays.toString(a));
int temp;
for(int i=0;i<a.length-1;i++){
for(int j=0;j<a.length-1;j++){
if(a[j] > a[j+1]){ // use < for Descending order
temp = a[j+1];
a[j+1] = a[j];
a[j]=temp;
}
}
}
System.out.println(Arrays.toString(a));
Output:
[6, 2, 5, 1]
[1, 2, 5, 6]
add a comment |
I would recommend looking at Selection sort or Insertion sort if you aren't too worried about performance. Maybe that will give you some ideas.
add a comment |
Bubble sort can be used here:
//Time complexity: O(n^2)
public static int bubbleSort(final int arr) {
if (arr == null || arr.length <= 1) {
return arr;
}
for (int i = 0; i < arr.length; i++) {
for (int j = 1; j < arr.length - i; j++) {
if (arr[j - 1] > arr[j]) {
arr[j] = arr[j] + arr[j - 1];
arr[j - 1] = arr[j] - arr[j - 1];
arr[j] = arr[j] - arr[j - 1];
}
}
}
return arr;
}
add a comment |
Array Sorting without using built in functions in java ......just make new File unsing this name -> (ArraySorting.java) ..... Run the Project and Enjoy it !!!!!
import java.io.*;
import java.util.Arrays;
import java.util.Scanner;
public class ArraySorting
{
public static void main(String args)
{
int temp=0;
Scanner user_input=new Scanner(System.in);
System.out.println("enter Size of Array...");
int Size=user_input.nextInt();
int a=new int[Size];
System.out.println("Enter element Of an Array...");
for(int j=0;j<Size;j++)
{
a[j]=user_input.nextInt();
}
for(int index=0;index<a.length;index++)
{
for(int j=index+1;j<a.length;j++)
{
if(a[index] > a[j] )
{
temp = a[index];
a[index] = a[j];
a[j] = temp;
}
}
}
System.out.print("Output is:- ");
for(int i=0;i<a.length;i++)
{
System.out.println(a[i]);
}
}
}
add a comment |
int x = { 10, 30, 15, 69, 52, 89, 5 };
int max, temp = 0, index = 0;
for (int i = 0; i < x.length; i++) {
int counter = 0;
max = x[i];
for (int j = i + 1; j < x.length; j++) {
if (x[j] > max) {
max = x[j];
index = j;
counter++;
}
}
if (counter > 0) {
temp = x[index];
x[index] = x[i];
x[i] = temp;
}
}
for (int i = 0; i < x.length; i++) {
System.out.println(x[i]);
}
add a comment |
int arr = {111, 111, 110, 101, 101, 102, 115, 112};
/* for ascending order */
System.out.println(Arrays.toString(getSortedArray(arr)));
/*for descending order */
System.out.println(Arrays.toString(getSortedArray(arr)));
private int getSortedArray(int k){
int localIndex =0;
for(int l=1;l<k.length;l++){
if(l>1){
localIndex = l;
while(true){
k = swapelement(k,l);
if(l-- == 1)
break;
}
l = localIndex;
}else
k = swapelement(k,l);
}
return k;
}
private int swapelement(int ar,int in){
int temp =0;
if(ar[in]<ar[in-1]){
temp = ar[in];
ar[in]=ar[in-1];
ar[in-1] = temp;
}
return ar;
}
private int getDescOrder(int byt){
int s =-1;
for(int i = byt.length-1;i>=0;--i){
int k = i-1;
while(k >= 0){
if(byt[i]>byt[k]){
s = byt[k];
byt[k] = byt[i];
byt[i] = s;
}
k--;
}
}
return byt;
}
output:-
ascending order:-
101, 101, 102, 110, 111, 111, 112, 115
descending order:-
115, 112, 111, 111, 110, 102, 101, 101
add a comment |
class Sort
{
public static void main(String args)
{
System.out.println("Enter the range");
java.util.Scanner sc=new java.util.Scanner(System.in);
int n=sc.nextInt();
int arr=new int[n];
System.out.println("Enter the array values");
for(int i=0;i<=n-1;i++)
{
arr[i]=sc.nextInt();
}
System.out.println("Before sorting array values are");
for(int i=0;i<=n-1;i++)
{
System.out.println(arr[i]);
}
System.out.println();
for(int pass=1;pass<=n;pass++)
{
for(int i=0;i<=n-1;i++)
{
if(i==n-1)
{
break;
}
int temp;
if(arr[i]>arr[i+1])
{
temp=arr[i];
arr[i]=arr[i+1];
arr[i+1]=temp;
}
}
}
System.out.println("After sorting array values are");
for(int i=0;i<=n-1;i++)
{
System.out.println(arr[i]);
}
}
}
This is my logic go through it
– saroj kumar patnaik
Mar 10 '15 at 15:18
add a comment |
here is the Sorting Simple Example try it
public class SortingSimpleExample {
public static void main(String args) {
int a={10,20,1,5,4,20,6,4,2,5,4,6,8,-5,-1};
a=sort(a);
for(int i:a)
System.out.println(i);
}
public static int sort(int a){
for(int i=0;i<a.length;i++){
for(int j=0;j<a.length;j++){
int temp=0;
if(a[i]<a[j]){
temp=a[j];
a[j]=a[i];
a[i]=temp;
}
}
}
return a;
}
}
add a comment |
This will surely help you.
int n = {4,6,9,1,7};
for(int i=n.length;i>=0;i--){
for(int j=0;j<n.length-1;j++){
if(n[j] > n[j+1]){
swapNumbers(j,j+1,n);
}
}
}
printNumbers(n);
}
private static void swapNumbers(int i, int j, int array) {
int temp;
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
private static void printNumbers(int input) {
for (int i = 0; i < input.length; i++) {
System.out.print(input[i] + ", ");
}
System.out.println("n");
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f13548619%2fjava-sort-integer-array-without-using-arrays-sort%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
12 Answers
12
active
oldest
votes
12 Answers
12
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can find so many different sorting algorithms in internet, but if you want to fix your own solution you can do following changes in your code:
Instead of:
orderedNums[greater]=tenNums[indexL];
you need to do this:
while (orderedNums[greater] == tenNums[indexL]) {
greater++;
}
orderedNums[greater] = tenNums[indexL];
This code basically checks if that particular index is occupied by a similar number, then it will try to find next free index.
Note: Since the default value in your sorted array elements is 0, you need to make sure 0 is not in your list. otherwise you need
to initiate your sorted array with an especial number that you sure is
not in your list e.g: Integer.MAX_VALUE
Thanks a lot mohammad, I spent too much time trying to formulate different algorithms, I didn't realize how simple it was that I just needed to add that one line to my code.
– ransan32
Nov 25 '12 at 15:53
add a comment |
You can find so many different sorting algorithms in internet, but if you want to fix your own solution you can do following changes in your code:
Instead of:
orderedNums[greater]=tenNums[indexL];
you need to do this:
while (orderedNums[greater] == tenNums[indexL]) {
greater++;
}
orderedNums[greater] = tenNums[indexL];
This code basically checks if that particular index is occupied by a similar number, then it will try to find next free index.
Note: Since the default value in your sorted array elements is 0, you need to make sure 0 is not in your list. otherwise you need
to initiate your sorted array with an especial number that you sure is
not in your list e.g: Integer.MAX_VALUE
Thanks a lot mohammad, I spent too much time trying to formulate different algorithms, I didn't realize how simple it was that I just needed to add that one line to my code.
– ransan32
Nov 25 '12 at 15:53
add a comment |
You can find so many different sorting algorithms in internet, but if you want to fix your own solution you can do following changes in your code:
Instead of:
orderedNums[greater]=tenNums[indexL];
you need to do this:
while (orderedNums[greater] == tenNums[indexL]) {
greater++;
}
orderedNums[greater] = tenNums[indexL];
This code basically checks if that particular index is occupied by a similar number, then it will try to find next free index.
Note: Since the default value in your sorted array elements is 0, you need to make sure 0 is not in your list. otherwise you need
to initiate your sorted array with an especial number that you sure is
not in your list e.g: Integer.MAX_VALUE
You can find so many different sorting algorithms in internet, but if you want to fix your own solution you can do following changes in your code:
Instead of:
orderedNums[greater]=tenNums[indexL];
you need to do this:
while (orderedNums[greater] == tenNums[indexL]) {
greater++;
}
orderedNums[greater] = tenNums[indexL];
This code basically checks if that particular index is occupied by a similar number, then it will try to find next free index.
Note: Since the default value in your sorted array elements is 0, you need to make sure 0 is not in your list. otherwise you need
to initiate your sorted array with an especial number that you sure is
not in your list e.g: Integer.MAX_VALUE
edited Feb 28 '15 at 19:29
gvlasov
6,581164984
6,581164984
answered Nov 25 '12 at 6:13
mhshamsmhshams
8,955124362
8,955124362
Thanks a lot mohammad, I spent too much time trying to formulate different algorithms, I didn't realize how simple it was that I just needed to add that one line to my code.
– ransan32
Nov 25 '12 at 15:53
add a comment |
Thanks a lot mohammad, I spent too much time trying to formulate different algorithms, I didn't realize how simple it was that I just needed to add that one line to my code.
– ransan32
Nov 25 '12 at 15:53
Thanks a lot mohammad, I spent too much time trying to formulate different algorithms, I didn't realize how simple it was that I just needed to add that one line to my code.
– ransan32
Nov 25 '12 at 15:53
Thanks a lot mohammad, I spent too much time trying to formulate different algorithms, I didn't realize how simple it was that I just needed to add that one line to my code.
– ransan32
Nov 25 '12 at 15:53
add a comment |
Simple sorting algorithm Bubble sort:
public static void main(String args) {
int arr = new int { 6, 8, 7, 4, 312, 78, 54, 9, 12, 100, 89, 74 };
for (int i = 0; i < arr.length; i++) {
for (int j = i + 1; j < arr.length; j++) {
int tmp = 0;
if (arr[i] > arr[j]) {
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
}
}
add a comment |
Simple sorting algorithm Bubble sort:
public static void main(String args) {
int arr = new int { 6, 8, 7, 4, 312, 78, 54, 9, 12, 100, 89, 74 };
for (int i = 0; i < arr.length; i++) {
for (int j = i + 1; j < arr.length; j++) {
int tmp = 0;
if (arr[i] > arr[j]) {
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
}
}
add a comment |
Simple sorting algorithm Bubble sort:
public static void main(String args) {
int arr = new int { 6, 8, 7, 4, 312, 78, 54, 9, 12, 100, 89, 74 };
for (int i = 0; i < arr.length; i++) {
for (int j = i + 1; j < arr.length; j++) {
int tmp = 0;
if (arr[i] > arr[j]) {
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
}
}
Simple sorting algorithm Bubble sort:
public static void main(String args) {
int arr = new int { 6, 8, 7, 4, 312, 78, 54, 9, 12, 100, 89, 74 };
for (int i = 0; i < arr.length; i++) {
for (int j = i + 1; j < arr.length; j++) {
int tmp = 0;
if (arr[i] > arr[j]) {
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
}
}
edited Feb 28 '15 at 19:25
gvlasov
6,581164984
6,581164984
answered Feb 28 '15 at 19:02
YogaYoga
18117
18117
add a comment |
add a comment |
Here is one simple solution
public static void main(String args) {
//Without using Arrays.sort function
int i;
int nos = {12,9,-4,-1,3,10,34,12,11};
System.out.print("Values before sorting: n");
for(i = 0; i < nos.length; i++)
System.out.println( nos[i]+" ");
sort(nos, nos.length);
System.out.print("Values after sorting: n");
for(i = 0; i <nos.length; i++){
System.out.println(nos[i]+" ");
}
}
private static void sort(int nos, int n) {
for (int i = 1; i < n; i++){
int j = i;
int B = nos[i];
while ((j > 0) && (nos[j-1] > B)){
nos[j] = nos[j-1];
j--;
}
nos[j] = B;
}
}
And the output is:
Values before sorting:
12
9
-4
-1
3
10
34
12
11
Values after sorting:
-4
-1
3
9
10
11
12
12
34
add a comment |
Here is one simple solution
public static void main(String args) {
//Without using Arrays.sort function
int i;
int nos = {12,9,-4,-1,3,10,34,12,11};
System.out.print("Values before sorting: n");
for(i = 0; i < nos.length; i++)
System.out.println( nos[i]+" ");
sort(nos, nos.length);
System.out.print("Values after sorting: n");
for(i = 0; i <nos.length; i++){
System.out.println(nos[i]+" ");
}
}
private static void sort(int nos, int n) {
for (int i = 1; i < n; i++){
int j = i;
int B = nos[i];
while ((j > 0) && (nos[j-1] > B)){
nos[j] = nos[j-1];
j--;
}
nos[j] = B;
}
}
And the output is:
Values before sorting:
12
9
-4
-1
3
10
34
12
11
Values after sorting:
-4
-1
3
9
10
11
12
12
34
add a comment |
Here is one simple solution
public static void main(String args) {
//Without using Arrays.sort function
int i;
int nos = {12,9,-4,-1,3,10,34,12,11};
System.out.print("Values before sorting: n");
for(i = 0; i < nos.length; i++)
System.out.println( nos[i]+" ");
sort(nos, nos.length);
System.out.print("Values after sorting: n");
for(i = 0; i <nos.length; i++){
System.out.println(nos[i]+" ");
}
}
private static void sort(int nos, int n) {
for (int i = 1; i < n; i++){
int j = i;
int B = nos[i];
while ((j > 0) && (nos[j-1] > B)){
nos[j] = nos[j-1];
j--;
}
nos[j] = B;
}
}
And the output is:
Values before sorting:
12
9
-4
-1
3
10
34
12
11
Values after sorting:
-4
-1
3
9
10
11
12
12
34
Here is one simple solution
public static void main(String args) {
//Without using Arrays.sort function
int i;
int nos = {12,9,-4,-1,3,10,34,12,11};
System.out.print("Values before sorting: n");
for(i = 0; i < nos.length; i++)
System.out.println( nos[i]+" ");
sort(nos, nos.length);
System.out.print("Values after sorting: n");
for(i = 0; i <nos.length; i++){
System.out.println(nos[i]+" ");
}
}
private static void sort(int nos, int n) {
for (int i = 1; i < n; i++){
int j = i;
int B = nos[i];
while ((j > 0) && (nos[j-1] > B)){
nos[j] = nos[j-1];
j--;
}
nos[j] = B;
}
}
And the output is:
Values before sorting:
12
9
-4
-1
3
10
34
12
11
Values after sorting:
-4
-1
3
9
10
11
12
12
34
edited Apr 17 '13 at 0:55
Jesse
6,88573755
6,88573755
answered Apr 17 '13 at 0:34
GopinathGopinath
211
211
add a comment |
add a comment |
Simple way :
int a={6,2,5,1};
System.out.println(Arrays.toString(a));
int temp;
for(int i=0;i<a.length-1;i++){
for(int j=0;j<a.length-1;j++){
if(a[j] > a[j+1]){ // use < for Descending order
temp = a[j+1];
a[j+1] = a[j];
a[j]=temp;
}
}
}
System.out.println(Arrays.toString(a));
Output:
[6, 2, 5, 1]
[1, 2, 5, 6]
add a comment |
Simple way :
int a={6,2,5,1};
System.out.println(Arrays.toString(a));
int temp;
for(int i=0;i<a.length-1;i++){
for(int j=0;j<a.length-1;j++){
if(a[j] > a[j+1]){ // use < for Descending order
temp = a[j+1];
a[j+1] = a[j];
a[j]=temp;
}
}
}
System.out.println(Arrays.toString(a));
Output:
[6, 2, 5, 1]
[1, 2, 5, 6]
add a comment |
Simple way :
int a={6,2,5,1};
System.out.println(Arrays.toString(a));
int temp;
for(int i=0;i<a.length-1;i++){
for(int j=0;j<a.length-1;j++){
if(a[j] > a[j+1]){ // use < for Descending order
temp = a[j+1];
a[j+1] = a[j];
a[j]=temp;
}
}
}
System.out.println(Arrays.toString(a));
Output:
[6, 2, 5, 1]
[1, 2, 5, 6]
Simple way :
int a={6,2,5,1};
System.out.println(Arrays.toString(a));
int temp;
for(int i=0;i<a.length-1;i++){
for(int j=0;j<a.length-1;j++){
if(a[j] > a[j+1]){ // use < for Descending order
temp = a[j+1];
a[j+1] = a[j];
a[j]=temp;
}
}
}
System.out.println(Arrays.toString(a));
Output:
[6, 2, 5, 1]
[1, 2, 5, 6]
answered Feb 22 '18 at 9:40
Premkumar ManipillaiPremkumar Manipillai
1,2871219
1,2871219
add a comment |
add a comment |
I would recommend looking at Selection sort or Insertion sort if you aren't too worried about performance. Maybe that will give you some ideas.
add a comment |
I would recommend looking at Selection sort or Insertion sort if you aren't too worried about performance. Maybe that will give you some ideas.
add a comment |
I would recommend looking at Selection sort or Insertion sort if you aren't too worried about performance. Maybe that will give you some ideas.
I would recommend looking at Selection sort or Insertion sort if you aren't too worried about performance. Maybe that will give you some ideas.
edited Feb 28 '15 at 19:30
gvlasov
6,581164984
6,581164984
answered Nov 25 '12 at 5:36
awolfe91awolfe91
1,5171710
1,5171710
add a comment |
add a comment |
Bubble sort can be used here:
//Time complexity: O(n^2)
public static int bubbleSort(final int arr) {
if (arr == null || arr.length <= 1) {
return arr;
}
for (int i = 0; i < arr.length; i++) {
for (int j = 1; j < arr.length - i; j++) {
if (arr[j - 1] > arr[j]) {
arr[j] = arr[j] + arr[j - 1];
arr[j - 1] = arr[j] - arr[j - 1];
arr[j] = arr[j] - arr[j - 1];
}
}
}
return arr;
}
add a comment |
Bubble sort can be used here:
//Time complexity: O(n^2)
public static int bubbleSort(final int arr) {
if (arr == null || arr.length <= 1) {
return arr;
}
for (int i = 0; i < arr.length; i++) {
for (int j = 1; j < arr.length - i; j++) {
if (arr[j - 1] > arr[j]) {
arr[j] = arr[j] + arr[j - 1];
arr[j - 1] = arr[j] - arr[j - 1];
arr[j] = arr[j] - arr[j - 1];
}
}
}
return arr;
}
add a comment |
Bubble sort can be used here:
//Time complexity: O(n^2)
public static int bubbleSort(final int arr) {
if (arr == null || arr.length <= 1) {
return arr;
}
for (int i = 0; i < arr.length; i++) {
for (int j = 1; j < arr.length - i; j++) {
if (arr[j - 1] > arr[j]) {
arr[j] = arr[j] + arr[j - 1];
arr[j - 1] = arr[j] - arr[j - 1];
arr[j] = arr[j] - arr[j - 1];
}
}
}
return arr;
}
Bubble sort can be used here:
//Time complexity: O(n^2)
public static int bubbleSort(final int arr) {
if (arr == null || arr.length <= 1) {
return arr;
}
for (int i = 0; i < arr.length; i++) {
for (int j = 1; j < arr.length - i; j++) {
if (arr[j - 1] > arr[j]) {
arr[j] = arr[j] + arr[j - 1];
arr[j - 1] = arr[j] - arr[j - 1];
arr[j] = arr[j] - arr[j - 1];
}
}
}
return arr;
}
answered Jul 28 '16 at 21:30
realPKrealPK
7271016
7271016
add a comment |
add a comment |
Array Sorting without using built in functions in java ......just make new File unsing this name -> (ArraySorting.java) ..... Run the Project and Enjoy it !!!!!
import java.io.*;
import java.util.Arrays;
import java.util.Scanner;
public class ArraySorting
{
public static void main(String args)
{
int temp=0;
Scanner user_input=new Scanner(System.in);
System.out.println("enter Size of Array...");
int Size=user_input.nextInt();
int a=new int[Size];
System.out.println("Enter element Of an Array...");
for(int j=0;j<Size;j++)
{
a[j]=user_input.nextInt();
}
for(int index=0;index<a.length;index++)
{
for(int j=index+1;j<a.length;j++)
{
if(a[index] > a[j] )
{
temp = a[index];
a[index] = a[j];
a[j] = temp;
}
}
}
System.out.print("Output is:- ");
for(int i=0;i<a.length;i++)
{
System.out.println(a[i]);
}
}
}
add a comment |
Array Sorting without using built in functions in java ......just make new File unsing this name -> (ArraySorting.java) ..... Run the Project and Enjoy it !!!!!
import java.io.*;
import java.util.Arrays;
import java.util.Scanner;
public class ArraySorting
{
public static void main(String args)
{
int temp=0;
Scanner user_input=new Scanner(System.in);
System.out.println("enter Size of Array...");
int Size=user_input.nextInt();
int a=new int[Size];
System.out.println("Enter element Of an Array...");
for(int j=0;j<Size;j++)
{
a[j]=user_input.nextInt();
}
for(int index=0;index<a.length;index++)
{
for(int j=index+1;j<a.length;j++)
{
if(a[index] > a[j] )
{
temp = a[index];
a[index] = a[j];
a[j] = temp;
}
}
}
System.out.print("Output is:- ");
for(int i=0;i<a.length;i++)
{
System.out.println(a[i]);
}
}
}
add a comment |
Array Sorting without using built in functions in java ......just make new File unsing this name -> (ArraySorting.java) ..... Run the Project and Enjoy it !!!!!
import java.io.*;
import java.util.Arrays;
import java.util.Scanner;
public class ArraySorting
{
public static void main(String args)
{
int temp=0;
Scanner user_input=new Scanner(System.in);
System.out.println("enter Size of Array...");
int Size=user_input.nextInt();
int a=new int[Size];
System.out.println("Enter element Of an Array...");
for(int j=0;j<Size;j++)
{
a[j]=user_input.nextInt();
}
for(int index=0;index<a.length;index++)
{
for(int j=index+1;j<a.length;j++)
{
if(a[index] > a[j] )
{
temp = a[index];
a[index] = a[j];
a[j] = temp;
}
}
}
System.out.print("Output is:- ");
for(int i=0;i<a.length;i++)
{
System.out.println(a[i]);
}
}
}
Array Sorting without using built in functions in java ......just make new File unsing this name -> (ArraySorting.java) ..... Run the Project and Enjoy it !!!!!
import java.io.*;
import java.util.Arrays;
import java.util.Scanner;
public class ArraySorting
{
public static void main(String args)
{
int temp=0;
Scanner user_input=new Scanner(System.in);
System.out.println("enter Size of Array...");
int Size=user_input.nextInt();
int a=new int[Size];
System.out.println("Enter element Of an Array...");
for(int j=0;j<Size;j++)
{
a[j]=user_input.nextInt();
}
for(int index=0;index<a.length;index++)
{
for(int j=index+1;j<a.length;j++)
{
if(a[index] > a[j] )
{
temp = a[index];
a[index] = a[j];
a[j] = temp;
}
}
}
System.out.print("Output is:- ");
for(int i=0;i<a.length;i++)
{
System.out.println(a[i]);
}
}
}
edited Aug 31 '16 at 11:38
mehrdad khosravi
2,14292131
2,14292131
answered Aug 31 '16 at 11:16
Adeel AsgharAdeel Asghar
14
14
add a comment |
add a comment |
int x = { 10, 30, 15, 69, 52, 89, 5 };
int max, temp = 0, index = 0;
for (int i = 0; i < x.length; i++) {
int counter = 0;
max = x[i];
for (int j = i + 1; j < x.length; j++) {
if (x[j] > max) {
max = x[j];
index = j;
counter++;
}
}
if (counter > 0) {
temp = x[index];
x[index] = x[i];
x[i] = temp;
}
}
for (int i = 0; i < x.length; i++) {
System.out.println(x[i]);
}
add a comment |
int x = { 10, 30, 15, 69, 52, 89, 5 };
int max, temp = 0, index = 0;
for (int i = 0; i < x.length; i++) {
int counter = 0;
max = x[i];
for (int j = i + 1; j < x.length; j++) {
if (x[j] > max) {
max = x[j];
index = j;
counter++;
}
}
if (counter > 0) {
temp = x[index];
x[index] = x[i];
x[i] = temp;
}
}
for (int i = 0; i < x.length; i++) {
System.out.println(x[i]);
}
add a comment |
int x = { 10, 30, 15, 69, 52, 89, 5 };
int max, temp = 0, index = 0;
for (int i = 0; i < x.length; i++) {
int counter = 0;
max = x[i];
for (int j = i + 1; j < x.length; j++) {
if (x[j] > max) {
max = x[j];
index = j;
counter++;
}
}
if (counter > 0) {
temp = x[index];
x[index] = x[i];
x[i] = temp;
}
}
for (int i = 0; i < x.length; i++) {
System.out.println(x[i]);
}
int x = { 10, 30, 15, 69, 52, 89, 5 };
int max, temp = 0, index = 0;
for (int i = 0; i < x.length; i++) {
int counter = 0;
max = x[i];
for (int j = i + 1; j < x.length; j++) {
if (x[j] > max) {
max = x[j];
index = j;
counter++;
}
}
if (counter > 0) {
temp = x[index];
x[index] = x[i];
x[i] = temp;
}
}
for (int i = 0; i < x.length; i++) {
System.out.println(x[i]);
}
answered Jan 12 '18 at 18:37
NarendraNarendra
918
918
add a comment |
add a comment |
int arr = {111, 111, 110, 101, 101, 102, 115, 112};
/* for ascending order */
System.out.println(Arrays.toString(getSortedArray(arr)));
/*for descending order */
System.out.println(Arrays.toString(getSortedArray(arr)));
private int getSortedArray(int k){
int localIndex =0;
for(int l=1;l<k.length;l++){
if(l>1){
localIndex = l;
while(true){
k = swapelement(k,l);
if(l-- == 1)
break;
}
l = localIndex;
}else
k = swapelement(k,l);
}
return k;
}
private int swapelement(int ar,int in){
int temp =0;
if(ar[in]<ar[in-1]){
temp = ar[in];
ar[in]=ar[in-1];
ar[in-1] = temp;
}
return ar;
}
private int getDescOrder(int byt){
int s =-1;
for(int i = byt.length-1;i>=0;--i){
int k = i-1;
while(k >= 0){
if(byt[i]>byt[k]){
s = byt[k];
byt[k] = byt[i];
byt[i] = s;
}
k--;
}
}
return byt;
}
output:-
ascending order:-
101, 101, 102, 110, 111, 111, 112, 115
descending order:-
115, 112, 111, 111, 110, 102, 101, 101
add a comment |
int arr = {111, 111, 110, 101, 101, 102, 115, 112};
/* for ascending order */
System.out.println(Arrays.toString(getSortedArray(arr)));
/*for descending order */
System.out.println(Arrays.toString(getSortedArray(arr)));
private int getSortedArray(int k){
int localIndex =0;
for(int l=1;l<k.length;l++){
if(l>1){
localIndex = l;
while(true){
k = swapelement(k,l);
if(l-- == 1)
break;
}
l = localIndex;
}else
k = swapelement(k,l);
}
return k;
}
private int swapelement(int ar,int in){
int temp =0;
if(ar[in]<ar[in-1]){
temp = ar[in];
ar[in]=ar[in-1];
ar[in-1] = temp;
}
return ar;
}
private int getDescOrder(int byt){
int s =-1;
for(int i = byt.length-1;i>=0;--i){
int k = i-1;
while(k >= 0){
if(byt[i]>byt[k]){
s = byt[k];
byt[k] = byt[i];
byt[i] = s;
}
k--;
}
}
return byt;
}
output:-
ascending order:-
101, 101, 102, 110, 111, 111, 112, 115
descending order:-
115, 112, 111, 111, 110, 102, 101, 101
add a comment |
int arr = {111, 111, 110, 101, 101, 102, 115, 112};
/* for ascending order */
System.out.println(Arrays.toString(getSortedArray(arr)));
/*for descending order */
System.out.println(Arrays.toString(getSortedArray(arr)));
private int getSortedArray(int k){
int localIndex =0;
for(int l=1;l<k.length;l++){
if(l>1){
localIndex = l;
while(true){
k = swapelement(k,l);
if(l-- == 1)
break;
}
l = localIndex;
}else
k = swapelement(k,l);
}
return k;
}
private int swapelement(int ar,int in){
int temp =0;
if(ar[in]<ar[in-1]){
temp = ar[in];
ar[in]=ar[in-1];
ar[in-1] = temp;
}
return ar;
}
private int getDescOrder(int byt){
int s =-1;
for(int i = byt.length-1;i>=0;--i){
int k = i-1;
while(k >= 0){
if(byt[i]>byt[k]){
s = byt[k];
byt[k] = byt[i];
byt[i] = s;
}
k--;
}
}
return byt;
}
output:-
ascending order:-
101, 101, 102, 110, 111, 111, 112, 115
descending order:-
115, 112, 111, 111, 110, 102, 101, 101
int arr = {111, 111, 110, 101, 101, 102, 115, 112};
/* for ascending order */
System.out.println(Arrays.toString(getSortedArray(arr)));
/*for descending order */
System.out.println(Arrays.toString(getSortedArray(arr)));
private int getSortedArray(int k){
int localIndex =0;
for(int l=1;l<k.length;l++){
if(l>1){
localIndex = l;
while(true){
k = swapelement(k,l);
if(l-- == 1)
break;
}
l = localIndex;
}else
k = swapelement(k,l);
}
return k;
}
private int swapelement(int ar,int in){
int temp =0;
if(ar[in]<ar[in-1]){
temp = ar[in];
ar[in]=ar[in-1];
ar[in-1] = temp;
}
return ar;
}
private int getDescOrder(int byt){
int s =-1;
for(int i = byt.length-1;i>=0;--i){
int k = i-1;
while(k >= 0){
if(byt[i]>byt[k]){
s = byt[k];
byt[k] = byt[i];
byt[i] = s;
}
k--;
}
}
return byt;
}
output:-
ascending order:-
101, 101, 102, 110, 111, 111, 112, 115
descending order:-
115, 112, 111, 111, 110, 102, 101, 101
edited Nov 20 '18 at 8:51
Suraj Rao
23.2k85770
23.2k85770
answered Nov 20 '18 at 8:46
Shubham GaurShubham Gaur
12
12
add a comment |
add a comment |
class Sort
{
public static void main(String args)
{
System.out.println("Enter the range");
java.util.Scanner sc=new java.util.Scanner(System.in);
int n=sc.nextInt();
int arr=new int[n];
System.out.println("Enter the array values");
for(int i=0;i<=n-1;i++)
{
arr[i]=sc.nextInt();
}
System.out.println("Before sorting array values are");
for(int i=0;i<=n-1;i++)
{
System.out.println(arr[i]);
}
System.out.println();
for(int pass=1;pass<=n;pass++)
{
for(int i=0;i<=n-1;i++)
{
if(i==n-1)
{
break;
}
int temp;
if(arr[i]>arr[i+1])
{
temp=arr[i];
arr[i]=arr[i+1];
arr[i+1]=temp;
}
}
}
System.out.println("After sorting array values are");
for(int i=0;i<=n-1;i++)
{
System.out.println(arr[i]);
}
}
}
This is my logic go through it
– saroj kumar patnaik
Mar 10 '15 at 15:18
add a comment |
class Sort
{
public static void main(String args)
{
System.out.println("Enter the range");
java.util.Scanner sc=new java.util.Scanner(System.in);
int n=sc.nextInt();
int arr=new int[n];
System.out.println("Enter the array values");
for(int i=0;i<=n-1;i++)
{
arr[i]=sc.nextInt();
}
System.out.println("Before sorting array values are");
for(int i=0;i<=n-1;i++)
{
System.out.println(arr[i]);
}
System.out.println();
for(int pass=1;pass<=n;pass++)
{
for(int i=0;i<=n-1;i++)
{
if(i==n-1)
{
break;
}
int temp;
if(arr[i]>arr[i+1])
{
temp=arr[i];
arr[i]=arr[i+1];
arr[i+1]=temp;
}
}
}
System.out.println("After sorting array values are");
for(int i=0;i<=n-1;i++)
{
System.out.println(arr[i]);
}
}
}
This is my logic go through it
– saroj kumar patnaik
Mar 10 '15 at 15:18
add a comment |
class Sort
{
public static void main(String args)
{
System.out.println("Enter the range");
java.util.Scanner sc=new java.util.Scanner(System.in);
int n=sc.nextInt();
int arr=new int[n];
System.out.println("Enter the array values");
for(int i=0;i<=n-1;i++)
{
arr[i]=sc.nextInt();
}
System.out.println("Before sorting array values are");
for(int i=0;i<=n-1;i++)
{
System.out.println(arr[i]);
}
System.out.println();
for(int pass=1;pass<=n;pass++)
{
for(int i=0;i<=n-1;i++)
{
if(i==n-1)
{
break;
}
int temp;
if(arr[i]>arr[i+1])
{
temp=arr[i];
arr[i]=arr[i+1];
arr[i+1]=temp;
}
}
}
System.out.println("After sorting array values are");
for(int i=0;i<=n-1;i++)
{
System.out.println(arr[i]);
}
}
}
class Sort
{
public static void main(String args)
{
System.out.println("Enter the range");
java.util.Scanner sc=new java.util.Scanner(System.in);
int n=sc.nextInt();
int arr=new int[n];
System.out.println("Enter the array values");
for(int i=0;i<=n-1;i++)
{
arr[i]=sc.nextInt();
}
System.out.println("Before sorting array values are");
for(int i=0;i<=n-1;i++)
{
System.out.println(arr[i]);
}
System.out.println();
for(int pass=1;pass<=n;pass++)
{
for(int i=0;i<=n-1;i++)
{
if(i==n-1)
{
break;
}
int temp;
if(arr[i]>arr[i+1])
{
temp=arr[i];
arr[i]=arr[i+1];
arr[i+1]=temp;
}
}
}
System.out.println("After sorting array values are");
for(int i=0;i<=n-1;i++)
{
System.out.println(arr[i]);
}
}
}
edited Mar 10 '15 at 15:32
Uri Agassi
32.4k106081
32.4k106081
answered Mar 10 '15 at 15:17
saroj kumar patnaiksaroj kumar patnaik
1
1
This is my logic go through it
– saroj kumar patnaik
Mar 10 '15 at 15:18
add a comment |
This is my logic go through it
– saroj kumar patnaik
Mar 10 '15 at 15:18
This is my logic go through it
– saroj kumar patnaik
Mar 10 '15 at 15:18
This is my logic go through it
– saroj kumar patnaik
Mar 10 '15 at 15:18
add a comment |
here is the Sorting Simple Example try it
public class SortingSimpleExample {
public static void main(String args) {
int a={10,20,1,5,4,20,6,4,2,5,4,6,8,-5,-1};
a=sort(a);
for(int i:a)
System.out.println(i);
}
public static int sort(int a){
for(int i=0;i<a.length;i++){
for(int j=0;j<a.length;j++){
int temp=0;
if(a[i]<a[j]){
temp=a[j];
a[j]=a[i];
a[i]=temp;
}
}
}
return a;
}
}
add a comment |
here is the Sorting Simple Example try it
public class SortingSimpleExample {
public static void main(String args) {
int a={10,20,1,5,4,20,6,4,2,5,4,6,8,-5,-1};
a=sort(a);
for(int i:a)
System.out.println(i);
}
public static int sort(int a){
for(int i=0;i<a.length;i++){
for(int j=0;j<a.length;j++){
int temp=0;
if(a[i]<a[j]){
temp=a[j];
a[j]=a[i];
a[i]=temp;
}
}
}
return a;
}
}
add a comment |
here is the Sorting Simple Example try it
public class SortingSimpleExample {
public static void main(String args) {
int a={10,20,1,5,4,20,6,4,2,5,4,6,8,-5,-1};
a=sort(a);
for(int i:a)
System.out.println(i);
}
public static int sort(int a){
for(int i=0;i<a.length;i++){
for(int j=0;j<a.length;j++){
int temp=0;
if(a[i]<a[j]){
temp=a[j];
a[j]=a[i];
a[i]=temp;
}
}
}
return a;
}
}
here is the Sorting Simple Example try it
public class SortingSimpleExample {
public static void main(String args) {
int a={10,20,1,5,4,20,6,4,2,5,4,6,8,-5,-1};
a=sort(a);
for(int i:a)
System.out.println(i);
}
public static int sort(int a){
for(int i=0;i<a.length;i++){
for(int j=0;j<a.length;j++){
int temp=0;
if(a[i]<a[j]){
temp=a[j];
a[j]=a[i];
a[i]=temp;
}
}
}
return a;
}
}
answered Jun 6 '16 at 6:36
Srinivas NelloreSrinivas Nellore
91
91
add a comment |
add a comment |
This will surely help you.
int n = {4,6,9,1,7};
for(int i=n.length;i>=0;i--){
for(int j=0;j<n.length-1;j++){
if(n[j] > n[j+1]){
swapNumbers(j,j+1,n);
}
}
}
printNumbers(n);
}
private static void swapNumbers(int i, int j, int array) {
int temp;
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
private static void printNumbers(int input) {
for (int i = 0; i < input.length; i++) {
System.out.print(input[i] + ", ");
}
System.out.println("n");
}
add a comment |
This will surely help you.
int n = {4,6,9,1,7};
for(int i=n.length;i>=0;i--){
for(int j=0;j<n.length-1;j++){
if(n[j] > n[j+1]){
swapNumbers(j,j+1,n);
}
}
}
printNumbers(n);
}
private static void swapNumbers(int i, int j, int array) {
int temp;
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
private static void printNumbers(int input) {
for (int i = 0; i < input.length; i++) {
System.out.print(input[i] + ", ");
}
System.out.println("n");
}
add a comment |
This will surely help you.
int n = {4,6,9,1,7};
for(int i=n.length;i>=0;i--){
for(int j=0;j<n.length-1;j++){
if(n[j] > n[j+1]){
swapNumbers(j,j+1,n);
}
}
}
printNumbers(n);
}
private static void swapNumbers(int i, int j, int array) {
int temp;
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
private static void printNumbers(int input) {
for (int i = 0; i < input.length; i++) {
System.out.print(input[i] + ", ");
}
System.out.println("n");
}
This will surely help you.
int n = {4,6,9,1,7};
for(int i=n.length;i>=0;i--){
for(int j=0;j<n.length-1;j++){
if(n[j] > n[j+1]){
swapNumbers(j,j+1,n);
}
}
}
printNumbers(n);
}
private static void swapNumbers(int i, int j, int array) {
int temp;
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
private static void printNumbers(int input) {
for (int i = 0; i < input.length; i++) {
System.out.print(input[i] + ", ");
}
System.out.println("n");
}
edited Dec 15 '16 at 21:21
Khalil M
93021327
93021327
answered Oct 9 '15 at 14:31
Krishna Kumar ChourasiyaKrishna Kumar Chourasiya
1,26421620
1,26421620
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f13548619%2fjava-sort-integer-array-without-using-arrays-sort%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Would it be cheating to create a PriorityQueue, add all the items, and then repeatedly poll until the queue is empty?
– Jim Mischel
Aug 31 '16 at 11:39