Advertisement
Stefi16524

Untitled

Jan 21st, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. package stefi;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Iskam6 {
  6.  
  7. public static void main(String[] args) {
  8. Scanner scan = new Scanner(System.in);
  9. String[] numbers = scan.next().split(", ");
  10. int[] nums = new int[numbers.length];
  11. for (int i = 0; i < nums.length; i++) {
  12. nums[i] = Integer.parseInt(numbers[i]);
  13. }
  14. int max = nums[0];
  15. int min = nums[0];
  16.  
  17. for (int i = 0; i < nums.length; i++) {
  18. if (max < nums[i]) {
  19. max = nums[i];
  20. }
  21. if (max > nums[i]) {
  22. max = nums[i];
  23. }
  24. }
  25. int sum = max + min;
  26. BubbleSortArray(nums);
  27. System.out.println("Sorted array: ");
  28. for (int n : nums) {
  29. System.out.print(n + " ");
  30.  
  31. }
  32. System.out.println();
  33. System.out.println("Sum = " + sum);
  34. if (LinearSearch(nums, 15) == -1) {
  35. System.out.println("Not found");
  36. } else {
  37. System.out.println("Index = " + LinearSearch(nums, 15));
  38. }
  39. }
  40.  
  41. static void BubbleSortArray(int[] array) {
  42. for (int j = 0; j < array.length; j++) {
  43. for (int i = 0; i < array.length; i++) {
  44. if (array[i] < array[i + 1]) {
  45. int temp = array[i];
  46. array[i] = array[i + 1];
  47. array[i + 1] = temp;
  48. }
  49. }
  50. }
  51. }
  52.  
  53. static int LinearSearch(int[] arr, int num) {
  54. for (int i = 0; i < arr.length; i++) {
  55. if (arr[i] == num) {
  56. return 1;
  57. }
  58. }
  59. return -1;
  60. }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement