Created
September 17, 2015 21:37
-
-
Save AnnaBoro/02faf558a65380be2190 to your computer and use it in GitHub Desktop.
lesson3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package lesson3; | |
import java.util.Arrays; | |
public class Frame19 { | |
public static void main(String[] args) { | |
int[] stack1 = {10, 2, 36, 4}; | |
int[] stack2 = {66, 222, 0, 44, 0}; | |
int[] stack3 = {0, -15, -3, 2}; | |
swap(stack1); | |
swap(stack2); | |
swap(stack3); | |
System.out.println(Arrays.toString(stack1)); | |
System.out.println(Arrays.toString(stack2)); | |
System.out.println(Arrays.toString(stack3)); | |
} | |
static void swap(int[] data) { | |
int temp; | |
if (data.length > 0) { | |
for(int i = 0; i < data.length; i++) { | |
for(int k = 0; k < data.length - 1; k++) { | |
if (data[i] < data[k]) { | |
temp = data[k]; | |
data[k] = data[i]; | |
data[i] = temp; | |
} | |
} | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package lesson3; | |
import java.util.Arrays; | |
public class Frame22 { | |
public static void main(String[] args) { | |
int[] data1 = {22, 3, 2, 1}; | |
int[] data2 = {200, 13, 30, 0}; | |
sort(data1); | |
sort(data2); | |
System.out.println(Arrays.toString(data1)); | |
System.out.println(Arrays.toString(data2)); | |
} | |
static void sort(int[] data) { | |
// long time = System.currentTimeMillis(); | |
for(int i = 0; i < data.length - 1; i++) { | |
swap(data); | |
} | |
// System.out.println((System.currentTimeMillis() - time)); | |
} | |
static void swap(int[] data) { | |
int temp; | |
if (data.length > 0) { | |
for(int i = 0; i < data.length - 1; i++) { | |
if (data[i] > data[i+1]) { | |
temp = data[i]; | |
data[i] = data[i+1]; | |
data[i+1] = temp; | |
} | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package lesson3; | |
import java.util.Arrays; | |
public class Frame23 { | |
public static void main(String[] args) { | |
int[] data1 = {200, 13, 30, 0, 99, 8, 200, 344, 444, 3, 48, 83, 93, 5059, 34, 334}; | |
int[] data2 = createArray(1000); | |
sort(data1); | |
sort(data2); | |
System.out.println(Arrays.toString(data1)); | |
System.out.println(Arrays.toString(data2)); | |
} | |
static void sort(int[] data) { | |
long time = System.currentTimeMillis(); | |
for(int i = 0; i < data.length - 1; i++) { | |
swap(data, i); | |
} | |
System.out.println(System.currentTimeMillis() - time); | |
} | |
static void swap(int[] data, int start) { | |
int temp; | |
if (data.length > 0) { | |
for(int i = 0; i < (data.length - start - 1); i++) { | |
if (data[i] > data[i+1]) { | |
temp = data[i]; | |
data[i] = data[i+1]; | |
data[i+1] = temp; | |
} | |
} | |
} | |
} | |
static int[] createArray(int dimens) { | |
int[] data = new int[dimens]; | |
for(int i = 0; i < data.length; i++) { | |
data[i] = dimens--; | |
} | |
return data; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package lesson3; | |
public class Frame26 { | |
public static void main(String[] args) { | |
String[][] country = new String[6][]; | |
country[0] = new String[] {"China", "Peking", "1354040000"}; | |
country[1] = new String[] {"India", "New Delhi", "1210569573"}; | |
country[2] = new String[] {"USA", "Wash", "316305000"}; | |
country[3] = new String[] {"Indonezia", "Jakarta", "237000000"}; | |
country[4] = new String[] {"Brazil", "Brazilia", "193000000"}; | |
country[5] = new String[] {"Pakistan", "Islamabad", "183040000"}; | |
System.out.println(getPopulation(country)); | |
} | |
static long getPopulation(String[][] data) { | |
long population = 0; | |
for (int i = 0, j = 2; i < data.length; i++) { | |
population = Integer.parseInt(data[i][j]) + population; | |
} | |
return population; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package lesson3; | |
import java.util.Arrays; | |
public class Frame27 { | |
public static void main(String[] args) { | |
int[] data1 = {2, 4, 6}; | |
int[] data2 = {4, 9, 1, 3}; | |
System.out.println(Arrays.toString(union(data1, data2))); | |
} | |
static int[] union(int[] data1, int[] data2) { | |
int[] newData = new int[data1.length + data2.length]; | |
int i = 0; | |
for (int j = 0; j < data1.length; j++) { | |
newData[i] = data1[j]; | |
i++; | |
} | |
for (int k = 0; k < data2.length; k++) { | |
newData[i] = data2[k]; | |
i++; | |
} | |
return newData; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment